Count the string
2017-03-25 15:14
211 查看
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.
InputThe 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.
OutputFor each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.Sample Input
Sample Output
6
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 200010
using namespace std;
char s[maxn];
int ne[maxn];
int num[maxn];
void get_next()
{
int i=0,j=-1;
ne[0]=-1;
for(; s[i];)
if(j==-1||s[i]==s[j])
{
++i;
++j;
ne[i]=j;
}
else j=ne[j];
}
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(num,0,sizeof(num));
getchar();
gets(s);
get_next();
for(int i=1;i<=n;i++){
num[ne[i]]=(num[ne[i]]+1)%10007;
}
int sum=0;
for(int i=1;i<=n;i++){
if(num[i]) sum=(sum+num[i]+1)%10007;
else sum=(sum+1)%10007;
}
printf("%d\n",sum);
}
}
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.
InputThe 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.
OutputFor 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
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 200010
using namespace std;
char s[maxn];
int ne[maxn];
int num[maxn];
void get_next()
{
int i=0,j=-1;
ne[0]=-1;
for(; s[i];)
if(j==-1||s[i]==s[j])
{
++i;
++j;
ne[i]=j;
}
else j=ne[j];
}
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(num,0,sizeof(num));
getchar();
gets(s);
get_next();
for(int i=1;i<=n;i++){
num[ne[i]]=(num[ne[i]]+1)%10007;
}
int sum=0;
for(int i=1;i<=n;i++){
if(num[i]) sum=(sum+num[i]+1)%10007;
else sum=(sum+1)%10007;
}
printf("%d\n",sum);
}
}
相关文章推荐
- hdu 3336 Count the string KMP+DP
- HDU 3336 Count the string(乱搞)
- HDU - 3336 Count the string —— next数组求相同前缀后缀个数
- K - Count the string kmp_Next数组应用
- K - Count the string(KMP寻找子串个数)
- hdu 3336 Count the string(next数组)
- hdu 3336 Count the string
- hdu 3336 Count the string (kmp + dp)
- hdu 3336:Count the string(数据结构,串,KMP算法)
- (HDU)3336 - Count the string 【KMP】+【DP】
- [KMP-NEXT数组特性]HDU 3336 Count the string
- HDU 3336--Count the string【KMP】
- HDU 3336 Count the string(KMP+dp)
- HDU Count the string
- HDU 3336 Count the string(kmp + dp)
- hdu 3336 Count the string(KMP+dp)
- hdu 3336 count the string
- hdu3336 Count the string 扩展KMP
- Count the string
- Count the string(kmp+dp)