HDU 3336 Count the string(next数组+DP)
2016-05-20 16:56
543 查看
Count the string
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
3336
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
题意:求给定字符串含前缀的数量
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个字符这一前缀
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
3336
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
题意:求给定字符串含前缀的数量
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 ; int nnext , dp ; 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; }
相关文章推荐
- ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint......
- 二叉搜索树变双向链表
- ACM--抛物线和直线围成的面积–-HDOJ 1071--The area--水
- struts2+jquery+json集成
- BestCoder Round #56 Clarke and problem
- ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint......
- tomcat管理脚本
- Unity 基础 - 刚体和 Collider
- WPF中Popup控件更换Child后位置显示不正确
- js跨域请求
- java-RCP
- Hibernate_OneToMany_Demo
- Android自定义控件之组合控件学习笔记分享
- C语言第十五篇:C语言中.h和.c文件解析(很精彩)
- 阿里面试算法题--版本号更新
- 面试感悟----一名3年工作经验的程序员应该具备的技能
- Table选项卡切换效果的实现
- 50个必备的实用jQuery代码段
- Centos 7 安装python2.7.11
- android camera 3d特效 详解与进阶