HDU 3336 (KMP next性质) Count the string

直接上传送门好了,我觉得他分析得非常透彻。

http://972169909-qq-com.iteye.com/blog/1114968

 1 #include <cstdio>
 2 #include <cstring>
 3
 4 const int maxn = 200000 + 10;
 5 const int MOD = 10007;
 6 char s[maxn];
 7 int next[maxn], l;
 8
 9 void get_next()
10 {
11     int k = -1, j = 0;
12     next[0] = -1;
13     while(j < l)
14     {
15         if(k == -1 || s[k] == s[j])
16         {
17             k++;
18             j++;
19             next[j] = k;
20         }
21         else k = next[k];
22     }
23 }
24
25 int main(void)
26 {
27     int T;
28     scanf("%d", &T);
29     while(T--)
30     {
31         scanf("%d", &l);
32         scanf("%s", s);
33         get_next();
34
35         int ans = 0;
36         for(int j = 0; j < l; ++j)
37             if(next[j] > 0 && next[j] + 1 != next[j+1])
38                 ans = (ans + next[j]) % MOD;
39         ans = (ans + l + next[l]) % MOD;
40         printf("%d\n", ans);
41     }
42
43     return 0;
44 }

代码君

时间: 2024-11-03 03:40:34

HDU 3336 (KMP next性质) Count the string的相关文章

【KMP+DP】Count the string

KMP算法的综合练习 DP很久没写搞了半天才明白.本题结合Next[]的意义以及动态规划考察对KMP算法的掌握. 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 str

hdu 3336 kmp+next数组应用

分析转自:http://972169909-qq-com.iteye.com/blog/1114968 十分易懂 题意:求字串中[前缀+跟前缀相同的子串]的个数? Sample Input 1 4 abab Sample Output 6 abab:包括2个a,2个ab,1个aba,1个abab 这里要用到next值的意义: next[i]表示前i个字符所组成的字符串的最大前后缀匹配长度 举个例子: next[5]=2, 表示下标5前面那个字符串abcab的前后缀匹配的最大长度是2,显然就是ab

hdu 3336 Count the string(KMP)

一道应用kmp算法中next数组的题目 这其中vis[i]从1加到n vis[i]=[next[i]]+1; #include<string.h> #include<stdlib.h> #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; char s[200005]; int b; int next[200005]; int vis[20000

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; ManacherK - Count the string HDU - 3336(前缀数量问题)

K - Count the string HDU - 3336 题目链接:https://vjudge.net/contest/70325#problem/K 题目: 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 t

(KMP)Count the string -- hdu -- 3336

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): 6672    Accepted Submission(s): 3089 Problem Description It is well known that Aek

hdu 3336 count the string(KMP+dp)

题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[i]表示以i为结尾包含前缀的数量,则dp[i]=dp[next[i]]+1,最后求和即可. #include <map> #include <set> #include <list> #include <cmath> #include <queue>

KMP——hdu 3336 count the string

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10478    Accepted Submission(s): 4893 Problem Description It is well known that AekdyCoin is good at string problems as well as n

HDU 3336 Count the string (KMP next数组运用——统计前缀出现次数)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6375    Accepted Submission(s): 2947 Problem Description It is well known that AekdyCoin is good at string problems as well as nu

hdu 3336 Count the string KMP+DP

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6375    Accepted Submission(s): 2947 Problem Description It is well known that AekdyCoin is good at string problems as well as n