第六次个人赛c题 count the string vector/kmp next数组...
时间:2010-08-07 来源:odday
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
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.
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.
Sample Input
1 4 ababSample 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 <LL> 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<n;i++) { if(s[i]==s[0]){ p.push_back(i); ans++; } } ans=ans%10007; for(i=1;i<n;i++){ for(j=0;j<p.size();j++){ if(s[i]==s[p[j]+i]) //用数组来做会超时 因为要释放节点,所以vector比较合适 ans=(ans+1)%10007; else{ p.erase(p.begin()+j); j--; } } } printf("%I64d\n",ans); } //system("pause"); return 0; } #include <iostream> #include <string.h> #include <string> 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 <iostream> 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); }
相关阅读 更多 +