Hdu-3336 Count the string(KMP + DP)
来源:互联网 发布:放空自己知乎 编辑:程序博客网 时间:2024/05/21 17:33
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7697 Accepted Submission(s): 3581
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
题意:问你字符串s的前缀一共出现了多少次。
分析:KMP求出next数组后DP。
#include <cstdio>#include <string>#include <cstring>#include <iostream>using namespace std;int T,n,ans[200005],Next[200005];char s[200005];int main(){scanf("%d",&T);while(T--){scanf("%d",&n);scanf("%s",&s);memset(Next,0,sizeof(Next));memset(ans,0,sizeof(ans)); int now = Next[0] = -1;int tot = 0;for(int i = 1;i < n;i++){while(now >= 0 && s[now+1] != s[i]) now = Next[now];if(s[now+1] == s[i]) now++;Next[i] = now;ans[now]++;}for(int i = n - 1;i;i--) if(Next[i] >= 0) ans[Next[i]] += ans[i];for(int i = n - 1;i >= 0;i--) tot = (tot + ans[i]) % 10007;tot = (tot + n) % 10007;printf("%d\n",tot);} }
0 0
- HDU 3336 Count the string(KMP+dp)
- [HDU 3336]Count the String[kmp][DP]
- HDU 3336 Count the string KMP+DP
- hdu 3336 Count the string KMP+DP
- HDU 3336 Count the string kmp+dp
- hdu -- 3336 Count the string(KMP + dp)
- hdu 3336 Count the string 【kmp + dp】
- hdu 3336 Count the string KMP+DP
- HDU 3336(Count the string)DP+KMP
- HDU 3336 Count the string (KMP + DP)
- hdu 3336 Count the string(KMP+dp)
- hdu 3336 Count the string (kmp + dp)
- Hdu-3336 Count the string(KMP + DP)
- HDU 3336 Count the string 【KMP】【dp】
- HDU 3336 Count the string (kmp+dp)
- HDU 3336 Count the string(经典,KMP+DP)
- hdu 3336 Count the string(KMP+dp)
- hdu 3336 Count the string (DP+kmp)
- CSU 国防科大决赛 D题 找路径 (水题-DP)
- 电池电压容量关系
- 开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
- JAVA性能调优点滴
- iOS DrawRect简单使用
- Hdu-3336 Count the string(KMP + DP)
- 一些C++经典书籍
- Qt实现应用单实例运行(3)-使用QTSingleApplication
- NSNotificationCenter
- 面试中的智力题及编程实践(二)
- MySQL创建字段+数据处理函数+汇总数据(聚集函数)+分组数据
- javase 基础 :变量
- 这个制作App的技能,让你酷到没朋友!
- Android弹性反弹:Facebook Rebound