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
【题意】给你一个字符串x,它有strlen(x)个前缀,问你所有前缀出现的次数总和,多组输入。
代码简单,先上代码。
【代码】
#include <bits/stdc++.h>
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=0;
for(i=0; i<n; i++)
{
k%=10007;
l=i;
j=0;
while(l<n)
if(s[l++]==s[j++])
k++;
else break;
}
cout<<k%10007<<endl;
}
return 0;
}
【分析】看不懂不急,我们慢慢看。首先是s,明显是存储字符串,k就是答案,然后关于输入部分就不解释了,关键就是那个循环的部分,那个if的条件意思是:如果字符串的s【0,j】和字符串s【i,l】相同,说明s【0,j】这个前缀出现了一次我们把他加上就行。然后i一循环答案就出来了。