HDU3336Count the string
来源:互联网 发布:曦力mac破解版 编辑:程序博客网 时间:2024/06/08 03:26
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8572 Accepted Submission(s): 3960
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
求前缀串在该串的出现次数
#include <iostream>#include <cstring>#include <cstdio>#define N 200010#define MOD 10007using namespace std;char s[N];int nextt[N],sum[N];int c,n,m;void get_next(){ int i=0,k=-1; nextt[0]=-1; while(i<m) { if(k==-1||s[i]==s[k]) { ++i,++k; nextt[i]=k; } else k=nextt[k]; }}int main(){ int t,ans; scanf("%d",&t); while(t--) { getchar(); scanf("%d",&n); ans = 0; memset(sum,0,sizeof(sum)); scanf("%s",s); m=strlen(s); get_next(); for(int i = 1; i<=n; i++) sum[nextt[i]] = (sum[nextt[i]]+1)%MOD; for(int i = 1; i<=n; i++) { if(sum[i]) ans=(ans+sum[i]+1)%MOD; else ans = (ans+1)%MOD; } printf("%d\n",ans); } return 0;}
- hdu3336Count the string
- hdu3336Count the string
- hdu3336Count the string kmp
- HDU3336Count the string
- hdu3336Count the string
- HDU3336Count the string
- HDU3336Count the string(KMP+动态规划)
- hdu3336Count the string(kmp的next的使用
- hdu4552怪盗基德的挑战书&hdu3336Count the string【后缀数组求各前缀重复次数和】
- hdu3336Count the string(KMP的next[]数组的应用+KMP模板)
- The String |
- The String ||
- the length of the string
- The C++ String Class
- the format control string
- hdu3336 Count the string
- hdu3336 Count the string
- HDU3336:Count the string
- 括号配对问题
- 生活中的数学 —— 几何
- selenium之 如何控制网页内嵌div中滚动条的滚动
- HDU 1712 ACboy needs your help
- Lightoj1282——Leading and Trailing(幂取模求前三位)
- HDU3336Count the string
- 求解最小数字:暴力破解 外加 对字符串的使用
- HDU 1811 Rank of Tetris (拓扑排序+并查集)
- Must Know Tips/Tricks in Deep Neural Networks
- Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()调用
- 最小生成树
- android 中include引用的误区。
- ios--网页js调用oc代码+传递参数+避免中文参数乱码的解决方案(实例)
- Linux服务器性能评估与优化(一)