Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8594 Accepted Submission(s): 3969
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.
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.
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
Recommend
TLE代码:
#include<cstdio> #include<iostream> using namespace std; #define N 200010 int T,lens,lenp,nextt[N]; char s[N],p[N]; inline void get_next(){ int i=0,j=-1; nextt[i]=j; while(i<lenp){ if(j==-1||p[i]==p[j]){ i++;j++; nextt[i]=j; } else j=nextt[j]; } } inline int kmp(){ get_next(); int i(0),j(0),ans(0); while(i<lens&&j<lenp){ if(j==-1||s[i]==p[j]){ i++;j++; } else j=nextt[j]; if(j==lenp) ans++,j=nextt[j]; } return ans%10007; } int main(){ scanf("%d",&T); while(T--){ scanf("%d",&lens); scanf("%s",s); int sum=0; for(int i=0;i<lens;i++){ fill(p,p+i+1,0); for(int j=0;j<=i;j++) p[j]=s[j]; lenp=i+1; sum+=kmp(); sum%=10007; } printf("%d ",sum%10007); } return 0; }
题解:
依次求字符串匹配数,求和,取模
水水的KMP
AC代码:
#include<cstdio> #include<iostream> using namespace std; #define N 200010//数组开的大大的 #define mod 10007//注意取模 int T,len,Next[N]; char s[N],p[N]; inline void get_next(){//next是关键字,所以用Next[] int i=0,j=-1; Next[i]=j; while(i<len){ if(j==-1||s[i]==s[j]){ i++;j++; Next[i]=j; } else j=Next[j]; } } int main(){ scanf("%d",&T); while(T--){ scanf("%d",&len); scanf("%s",s); get_next();//每组数据做一次求好了,否则TLE int sum=0; for(int i=1,j;i<=len;i++){ j=i; while(j) sum=(sum+1)%mod,j=Next[j]; } printf("%d ",sum); } return 0; }