第六次个人赛C题 Count the string vector/KMP next数组

Count the string

http://acm.hdu.edu.cn/showproblem.php?pid=3336

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 883    Accepted Submission(s): 382


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.
 


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
题意很简单,可以有重叠的字串计算,比如aaaa的答案是10。这题是KMP算法next数组的
运用, 但是好好想想就知道不至于那么复杂,下面是我的vector容器做法,还有附上
大牛的简单做法。KMP算法在最后面,后兴趣的可以研究一下:
#include "algorithm" #include "iostream" #include "cmath" #include "stdio.h" #include "string.h" #include "stdlib.h" #include "vector" typedef long long LL; using namespace std; char s[200001]; vector p; int main(){ int T; LL ans,i,n,j; scanf("%d",&T); while(T--){ ans=0; p.clear(); scanf("%I64d%s",&n,s); for(i=0;i
#include #include #include using namespace std; int sum; char str[200010]; int main () { int T; scanf ("%d",&T); while (T--) { scanf ("%d/n",&sum); gets (str); int n = sum; sum = sum % 10007; int k = 0; for (int i = 1; i < n; i++) { if (str[i] == str[0]) { k = 0; for (int j = i; str[j] == str[k++]; j++) { sum ++; sum %= 10007; } } } printf ("%d/n",sum); //puts (str); } return 0; }
#include using namespace std; typedef long long LL; const int MOD = 10007; const int MAXN = 200010; char s[MAXN]; int next[MAXN], f[MAXN]; void preKmp(char *pat, int next[], int m){ //preKmp和下面 getNext区别 getNext的next数组后移一位 int i, j; next[0] = j = -1; for(i = 1; i < m; ++i){ while(j > -1 && pat[j+1] != pat[i]) j = next[j]; if(pat[j+1] == pat[i]) ++j; next[i] = j; } } void solve(int n){ int i, j, ans = 0; preKmp(s, next, n); fill_n(f, n, 0); for(i = 0; i < n; ++i){ f[i] = 1; j = next[i]; if(j != -1) f[i] += f[j]; ans = (ans+f[i])%MOD; } printf("%d/n", ans); } int main(){ int T, n; scanf("%d", &T); while(T--){ scanf("%d%s", &n, s); solve(n); } return 0; } http://blog.163.com/northeastern_001/blog/static/1317852082009101485451354/ KMP abab next[4]=2 表示前4个字串的首尾最长匹配子串 长度为2 next[0]=-1,next[1]=0,next[2]=0,next[3]=1 next[i]可以描述为"不为自身的最大首尾重复子串长度"。 void getNext(char s[],int next[]) { int i=0,j=-1; next[0]=-1; while(s[i]!='/0') { if(j==-1||s[i]==s[j]) { ++i; ++j; //修正 if(s[i]==s[j]) next[i] = next[j]; else next[i]=j; } else j=next[j]; } } void solve(int n){ int i, j, ans = 0; getNext(s,next); fill_n(f, n, 0); for(i = 1; i <= n; ++i){ f[i] = 1; j = next[i]; f[i] += f[j]; ans = (ans+f[i])%MOD; } printf("%d/n", ans); }

你可能感兴趣的:(解题报告)