hdu 3336 Count the string
2012-07-23 09:44
204 查看
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1960 Accepted Submission(s): 923
[align=left]Problem Description[/align]
It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
s: "abab"
The prefixes are: "a", "ab", "aba", "abab"
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab",
it is 2 + 2 + 1 + 1 = 6.
The answer may be very large, so output the answer mod 10007.
[align=left]Input[/align]
The first line is a single integer T, indicating the number of test cases.
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.
[align=left]Output[/align]
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
[align=left]Sample Input[/align]
1 4 abab
[align=left]Sample Output[/align]
6
[align=left]Author[/align]
foreverlin@HNU
[align=left]Source[/align]
HDOJ Monthly Contest – 2010.03.06
[align=left]Recommend[/align]
lcy
转载地址:/article/6088007.html
有关DP那一段没怎么看懂,但还是转过来了,因为KMP那一段就是简单的套模板。一道典型的KMP的应用。
#include <stdio.h> #include <string.h> #include <stdlib.h> int m; char s[300000]; int a[1000005],next[300000]; void getnext() { int i=0,j=-1; next[0]=-1; while(i<m) { if(j==-1||s[j]==s[i]) { j++;i++; next[i]=j; } else j=next[j]; } }//模板 int main() { int sum,T; scanf("%d",&T); while(T--) { sum=0; scanf("%d",&m); scanf("%s",s); getnext(); memset(a,0,sizeof(a)); for(int i=1;i<=m;i++) { a[i]=(a[next[i]]+1)%10007;//有点没看懂,还是把别人的代码先贴出来。 sum=(sum+a[i])%10007; } printf("%d\n",sum); } }
相关文章推荐
- HDU 3336 Count the string (next数组活用)
- [HDU 3336]Count the String[kmp][DP]
- hdu 3336 Count the string(★)
- hdu 3336 Count the string
- HDU 3336 Count the string(kmp)
- hdu 3336【Count the string】(KMP)
- hdu -- 3336 Count the string(KMP + dp)
- HDU 3336 Count the String(KMP+DP)
- Count the string HDU - 3336 KMP+DP
- HDU 3336 Count the string(乱搞)
- HDU 3336 Count the string / KMP
- hdu 3336 Count the string KMP模式匹配 ★★★
- HDU 3336 Count the string(KMP)
- hdu 3336 Count the string
- hdu 3336 Count the string
- HDU 3336 Count the string (KMP + DP)
- HDU 3336 Count the string(kmp + dp)
- hdu 3336 count the string
- hdu 3336 Count the string
- HDU:3336-Count the string(next数组理解)