hdu3336 Count the string
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336
题目:
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9884 Accepted Submission(s): 4617
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
lcy
思路:next数组+dp
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4
5 using namespace std;
6
7 #define MP make_pair
8 #define PB push_back
9 typedef long long LL;
10 const double eps=1e-8;
11 const int K=1e6+7;
12 const int mod=1e9+7;
13
14 int nt[K],ans[K],dp[K];
15 char sa[K],sb[K];
16 void kmp_next(char *T,int *next)
17 {
18 next[0]=0;
19 for(int i=1,j=0,len=strlen(T);i<len;i++)
20 {
21 while(j&&T[i]!=T[j]) j=next[j-1];
22 if(T[i]==T[j]) j++;
23 next[i]=j;
24 }
25 }
26 int kmp(char *S,char *T,int *next)
27 {
28 int ans=0;
29 int ls=strlen(S),lt=strlen(T);
30 kmp_next(T,next);
31 for(int i=0,j=0;i<ls;i++)
32 {
33 while(j&&S[i]!=T[j]) j=next[j-1];
34 if(S[i]==T[j]) j++;
35 if(j==lt) ans++;
36 }
37 return ans;
38 }
39 int main(void)
40 {
41 int t,n;cin>>t;
42 while(t--)
43 {
44 int ans=0;
45 scanf("%d%s",&n,sa);
46 kmp_next(sa,nt);
47 dp[0]=1;
48 for(int i=1;i<n;i++)
49 dp[i]=dp[nt[i]-1]+1;
50 for(int i=0;i<n;i++)
51 ans=(ans+dp[i])%10007;
52 printf("%d\n",ans);
53 }
54 return 0;
55 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。