Count the string
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 26 Accepted Submission(s) : 13
Problem Description
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.
Input
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.
Output
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
Sample Input
1 4
abab
Sample Output
6
Author
foreverlin@HNU
Source
HDOJ Monthly Contest – 2010.03.06
t这一题目纯粹的KMP字符匹配,第一行输入T组测试样例。然后输入N,表示要输入一个长度为N的字符串。对于该字符串,把该字符串的一次分
解成1, 2,3...N长度的子串,然后拿去和主串匹配。把每一次能够匹配的次数累加起来,就是所要求的的答案、
1 #include2 #include 3 #include 4 char S[2000050],t[200005]; 5 int Len_S,Len_t,num[200005]; 6 void getNext(char p[],int next[]) 7 { 8 int j,k,L; 9 next[0]=-1;10 j=0;11 k=-1;12 while(j
模板更新(2016.4.20):
1 #include2 #include 3 #include 4 #include 5 using namespace std; 6 int Len; 7 void getNext(char p[],int next[],int Len_t) 8 { 9 int i=0,j=-1;10 next[0]=-1;11 while(i