Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3108 Accepted Submission(s): 1449
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.
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.
1 4 abab
6
#include <iostream>
#include <cstring>
using namespace std;
int main (void)
{
int n,m,i,j,k,l;
char s[222222];
cin>>m;
while(m--&&cin>>n)
{
cin>>s;k=n%10007;
for(i=1;i<n;i++)
{
l=i;j=0; //每次都是用j=0从第一个开始匹配,l=i代替i向后滑动
while(l<n&&j<n)
if(s[l++]==s[j++])
k=(k+1)%10007; //成功一个加一个
else break; //不成功变成仁,所以break
}
cout<<k<<endl;
}
return 0;
}