hdu 3336 Count the string -KMP&dp

  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

  可以确定肯定不能拿每个前缀跑去KMP,那么来回想一下fail(next)数组的意义——最长相同的前缀和后缀。对于长度为i的前缀,那么f[i]中包含两个长度为f[i]的前缀(大概就这个意思),用cnt[i]表示1-i这一段中有多少个子串与长度为f[i],f[f[i]]的前缀相同(除去cnt[f[i]]这一部分),于是可以得出

  cnt[i] = cnt[f[i]] + 1

  最后把所有cnt求个和就是最终答案

Code

 1 /**
 2  * hdu
 3  * Problem#3336
 4  * Accepted
 5  * Time:46ms
 6  * Memory:3316k
 7  */
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cctype>
11 #include<cstring>
12 #include<cstdlib>
13 #include<fstream>
14 #include<sstream>
15 #include<algorithm>
16 #include<map>
17 #include<ctime>
18 #include<set>
19 #include<queue>
20 #include<vector>
21 #include<stack>
22 using namespace std;
23 typedef bool boolean;
24 #define INF 0xfffffff
25 #define smin(a, b) a = min(a, b)
26 #define smax(a, b) a = max(a, b)
27 #ifndef WIN32
28 #define AUTO "%lld"
29 #else
30 #define AUTO "%I64d"
31 #endif
32 template<typename T>
33 inline void readInteger(T& u){
34     char x;
35     int aFlag = 1;
36     while(!isdigit((x = getchar())) && x != '-');
37     if(x == '-'){
38         x = getchar();
39         aFlag = -1;
40     }
41     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
42     ungetc(x, stdin);
43     u *= aFlag;
44 }
45 
46 int kase;
47 int n;
48 char s[200005];
49 int f[200005];
50 int cnt[200005];
51 
52 inline void init() {
53     readInteger(n);
54     gets(s);
55     gets(s);
56 }
57 
58 inline void solve() {
59     f[0] = f[1] = 0;
60     int j;
61     for(int i = 1; i < n; i++) {
62         j = f[i];
63         while(j > 0 && s[i] != s[j])    j = f[j];
64         f[i + 1] = (s[i] == s[j]) ? (j + 1) : (0);
65     }
66     int res = 0;
67     for(int i = 1; i <= n; i++)
68         (cnt[i] = cnt[f[i]] + 1) %= 10007, (res += cnt[i]) %= 10007;
69     printf("%d\n", res);
70 }
71 
72 int main() {
73     readInteger(kase);
74     while(kase--) {
75         init();
76         solve();
77     }
78     return 0;
79 }

转载于:https://www.cnblogs.com/yyf0309/p/6379890.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值