【HDU3336】【Count the string】
来源:互联网 发布:探索性空间数据分析 编辑:程序博客网 时间:2024/06/05 19:29
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6635 Accepted Submission(s): 3072
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
Author
foreverlin@HNU
Source
HDOJ Monthly Contest – 2010.03.06
Recommend
lcy | We have carefully selected several similar problems for you: 3746 3341 2222 3068 3339
dp[i] = dp[next[i]] + 1 // dp[i] 表示以a[i]结尾的符合要求的字符串
#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std; int T;int n;char str[200010];int Next[200010];int dp[200010];const int MOD = 10007;void getNext(){int i=0;int j = -1;Next[0] = -1;while(i < n){if(j == -1 || str[i] == str[j]){Next[++i] = ++j;}else{j = Next[j];}}}int sum;int main(){ scanf("%d",&T);while(T--){scanf("%d",&n);scanf("%s",str);getNext();memset(dp,0,sizeof(dp));sum = 0;for(int i=1;i<=n;i++){dp[i] = dp[Next[i]] + 1; (sum+= dp[i]) %= MOD;}printf("%d\n",sum);} return 0;}
0 0
- hdu3336 Count the string
- hdu3336 Count the string
- HDU3336:Count the string
- hdu3336-Count the string
- hdu3336 Count the string
- hdu3336 Count the string
- hdu3336----Count the string
- hdu3336 Count the string
- 【HDU3336】【Count the string】
- HDU3336 Count the string
- HDU3336-Count the string
- 【KMP】 hdu3336 Count the string
- hdu3336 Count the string-------KMP
- hdu3336 Count the string KMP
- [KMP][HDU3336][Count the string]
- HDU3336 Count the string (KMP)
- HDU3336-Count the string(KMP)
- hdu3336 Count the string KMP+dp
- mac上运行QT代码
- C++ <二分查找法>
- Gson解析JSON
- boost 最大流求解示例
- 谈谈Redis的SETNX
- 【HDU3336】【Count the string】
- Hessian学习总结(一)——Hessian入门
- 在Xcode6.4中添加空模板
- python日积月累之maketrans
- Android基础入门教程——7.6.1 Socket学习网络基础准备
- XPath解析xml
- 清除代码异味
- POJ - 2739 Sum of Consecutive Prime Numbers(尺取法)
- CentOS yum源 部署