【hdu 5635】LCP Array 中文题意&题解&代码(C++)
2016-03-07 20:30
423 查看
LCP Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Problem Description
Peter has a string s=s1s2…sn, let suffi=sisi+1…sn be the suffix start with i-th character of s. Peter knows the lcp (longest common prefix) of each two adjacent suffixes which denotes as ai=lcp(suffi,suffi+1).
Given the lcp array, Peter wants to know how many strings containing lowercase English letters only will satisfy the lcp array. The answer may be too large, just print it modulo 109+7.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains an integer n (2≤n≤105) – the length of the string. The second line contains n−1 integers: a1,a2,…,an−1 (0≤ai≤n).
The sum of values of n in all test cases doesn’t exceed 106.
Output
For each test case output one integer denoting the answer. The answer must be printed modulo 109+7.
Sample Input
3
3
0 0
4
3 2 1
3
1 2
Sample Output
16250
26
0
中文题意:
有一个数组a,a[i]表示一个字符串以第i位开头的后缀与以第i+1位为开头的后缀的最长公共前缀的长度,现在给你数组a, 问有多少个仅包含小写字母的字符串满足这个数组的性质. 答案也许会很大, 你只要输出对1000000007取模的结果.
题解:
将字符串用线段表示,画个图后很容易会发现假如a[i]不等于0则这个字符串从第i位开始一直到第i+a[i]位都必须是同一个字符,而且当a[i]不为零时a[i+1]一定等于a[i]-1,因此利用这个性质我们可以判断出是否存在满足数组a的字符串,那么该怎么统计个数呢,还是自己在本子上写写画画会发现只有当a[i]等于0的时候字符串的第i位与第i+1是不同的字符,然后就简单了,直接一遍扫过去,然而代码能力太差wa了好多次,最后发现脑残的在输入数据的判断时加了break。。。。
#include<iostream> #include<algorithm> #include<stdio.h> using namespace std; int flag,tot,T,n; long long ans; int main() { scanf("%d",&T); while(T--) { ans=26;tot=0;flag=0; int mmod=1000000007; int now=0,pre=0; scanf("%d",&n); for (int i=1;i<n;i++) { scanf("%d",&now); if ( now!=pre-1 && pre>0 ) flag=1; else if ( now==0 ) tot++; pre=now; } if (now>1) flag=1; //最后一位的最长公共前缀一定是0或1,不可能超过n-i //至于为什么不用在中间判断a[i]是否大于n-i,那是因为如果最后一位满足了上面的条件,而中间有一位超过了n-i,那么一定存在now!=pre-1 for (int i=1;i<=tot;i++) ans=(ans*25)%mmod; if (flag) printf("0\n"); else printf("%I64d\n",ans); } }
相关文章推荐
- C++ 知识点备忘
- C++模板插入排序
- Effictive C++笔记
- C/C++中的位运算(上)
- 深入学习C++.2016_3_7
- Cpp--拷贝构造函数
- 九度OJ 1207:质因数的个数
- new................未完待续
- C语言实现大数据除法
- C++内存布局(下)
- c++编译器对多态的实现原理总结
- YTU 2425: C语言习题 输出月份
- c语言技巧
- YTU 2420: C语言习题 不等长字符串排序
- YTU 2419: C语言习题 等长字符串排序
- C++关键字之:explicit
- YTU 2424: C语言习题 字符串比较
- C++ Builder 中去除字符串中的指定字符
- YTU 2417: C语言习题 字符串长度
- C++字符串学习(二)