HDU 3336 Count the string(next数组+DP)
来源:互联网 发布:java see link 编辑:程序博客网 时间:2024/05/22 17:51
Count the string
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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
题意:求给定字符串含前缀的数量
abab
前缀为
a
ab
aba
abab
abab中共有六个子串是前缀a a ab ab aba abab
所以答案为6
利用kmp中的匹配原理可以完美的解决此题
a---------d-----
-----a---------d
i j
如上所示,假设两串字符完全相等,next[j]=i,代表s[1...i]==sum[j-i+1....j],这一段其实就是前缀
i~j之间已经不可能有以j结尾的子串是前缀了,不然next【j】就不是 i 了
设dp【i】:以string[i]结尾的子串总共含前缀的数量
所以dp[j]=dp[i]+1,即以i结尾的子串中含前缀的数量加上前j个字符这一前缀
/** kmp+dp*/#include <cstdio>#include <iostream>#include <string>#include <cstring>using namespace std;const int N = 200005;char pat[N];int nnext[N], dp[N];void indexNext() { ///从第二个开始构造!!!挺巧妙的,学习了!!! int k = 0; nnext[1] = 0; for (int i=2; pat[i]; ++i) { while (k && pat[k+1]!=pat[i]) k = nnext[k]; if (pat[k+1] == pat[i]) ++k; nnext[i] = k; }}int solve(int len) { int sum = 0; //getnext(strlen(pat)); indexNext(); for (int i=1; i<=len; ++i) dp[i] = 1; dp[0] = 0; for (int i=1; i<=len; ++i) { dp[i] += dp[nnext[i]]; sum += dp[i]; sum %= 10007; } return sum;}int main() { int t; scanf ("%d", &t); pat[0] = '#'; while (t--) { int n; scanf ("%d%s", &n, pat+1); printf ("%d\n", solve(n)); } return 0;}
0 0
- HDU 3336 Count the string(next数组+DP)
- hdu 3336 Count the string(next数组+dp)
- HDU 3336 Count the string(KMP+稍微DP+next数组的运用)
- Count the string next数组+DP
- [KMP-NEXT数组特性]HDU 3336 Count the string
- Hdu 3336 Count the string[KMP next数组的理解]
- HDU 3336 Count the string (next数组活用)
- 【HDU 3336】Count the string 【KMP next数组巧妙应用】
- HDU 3336 Count the string(KMP算法next数组的应用)
- HDU 3336 Count the string(KMP+Next数组递推)
- HDU 3336 Count the string(kmp next数组的性质)
- hdu3336 Count the string(next数组)
- 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
- 命令行操作mysql,创建数据库,创建表
- 基于图的模型
- mybatis集成spring操作数据库(注解方式
- 窗体实验-猜数字
- Git 安装和配置(网络代理的设置)
- HDU 3336 Count the string(next数组+DP)
- 自定义字典表的使用
- ByteBuffer 到底怎么用?网络编程中一点总结!
- 如何使用Spark ALS实现协同过滤
- 50个必备的实用jQuery代码段
- QPushButton 的样式表代码
- Table选项卡切换效果的实现
- java中String.replaceAll()的使用
- 详解Android中AsyncTask的使用