hdu3336之KMP应用
来源:互联网 发布:pkpm是什么软件 编辑:程序博客网 时间:2024/05/16 00:50
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2987 Accepted Submission(s): 1391
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.
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.
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
14abab
Sample Output
6
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<iomanip>#define INF 99999999using namespace std;const int MAX=200000+10;const int mod=10007;char s[MAX];int next[MAX];void get_next(char *a,int len){int i=-1,j=0;next[0]=-1;while(j<len){if(i == -1 || a[i] == a[j])next[++j]=++i;else i=next[i];}return;}int main(){int t,n;cin>>t;while(t--){cin>>n>>s;get_next(s,n);int sum=0,k=0;for(int i=1;i<=n;++i){k=i;++sum;//加上前i个字串本身是一个匹配 while(next[k])sum=(sum+1)%mod,k=next[k];//加上第i-1个字符被匹配的次数 }cout<<sum<<endl;}return 0;}
- hdu3336之KMP应用
- hdu3336 KMP应用
- hdu3336 Count the string(KMP应用)
- hdu3336(KMP)
- HDU3336-KMP
- hdu3336 KMP
- hdu3336(kmp)
- HDU3336(KMP)
- hdu3336 KMP
- HDU3336 KMP之next数组妙用
- hdu3336(Count the string)KMP的应用
- ACM-KMP之Count the string——hdu3336
- HDU3336------KMP+DP
- hdu3336(KMP+DP)
- hdu3336(KMP+DP)
- hdu3336(kmp+dp)
- 【KMP】 hdu3336 Count the string
- hdu3336 Count the string-------KMP
- Msfpayload
- iOS并发处理的那些事(译自外文)
- SQL语句之单行函数
- properties editor for eclipse安装使用
- Study Linux - GDB调试
- hdu3336之KMP应用
- Start up JIAJIA
- PHP小功能-获取天气预报
- MAC 虚拟机安装过程问题解决
- Android中振动器(Vibrator)的使用
- phpeclipse Xdebug无法调试
- MyEclipse快捷键设置(全面升级版本) .
- Machine Learning(七)支持向量机
- mac下实用的代码编辑器