hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6062 Accepted Submission(s): 2810
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.
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.
#include<stdio.h>
#include<string.h>
#define mod 10007
#define MAX 200010
char p[MAX];
int f[MAX];
int dp[MAX];
void getfail()
{
int i,j;
f[0]=f[1]=0;
int len=strlen(p);
for(i=1;i<len;i++)
{
j=f[i];
while(j&&p[i]!=p[j])
j=f[j];
f[i+1]=p[i]==p[j]?j+1:0;
}
}
int main()
{
int n,m,j,i,s,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%s",p);
getfail();
s=0;
dp[0]=0;
for(i=1;i<=n;i++)
{
dp[i]=(dp[f[i]]%mod+1)%mod;
s=(s%mod+dp[i]%mod)%mod;
}
printf("%d\n",s);
}
return 0;
}
hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】的更多相关文章
- hdu 3336 Count the string KMP+DP优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- hud 3336 count the string (KMP)
这道题本来想对了,可是因为hdu对pascal语言的限制是我认为自己想错了,结果一看题解发现自己对了…… 题意:给以字符串 计算出以前i个字符为前缀的字符中 在主串中出现的次数和 如: num(aba ...
- [HDU 3336]Count the String[kmp][DP]
题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...
- hdu 3336 Count the string -KMP&dp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- HDU 3336 Count the string ( KMP next函数的应用 + DP )
dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ 3336 Count the string (KMP+DP,好题)
参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...
- ACM hdu 3336 Count the string
[题意概述] 给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和. [题目分析] 利用kmp算法的next数组 再加上dp [存在疑惑] 在分析nex ...
随机推荐
- IE6背景图片闪动问题
在IE6中,当JS触发事件时或者hover的时候,如果网速过慢 IE6背景图片重新加载会闪一下. 好的一个解决方案是 <!--[if IE 6]><script> try{do ...
- Python小杂点
1.函数调用 a = [] def fun(a): a.append(1) print (a) 这个时候输出的结果为[],如果想要出现1,需要先运行函数fun,然后在输出a 2.python方法 de ...
- 【资料目录收藏】.NET开发必看资料53个 经典源码77个
简单描述:为大家整理了下载中心.net资料,都是比较热的,好评率比较高的,做了一个可收藏的下载目录,希望大家喜欢~ 基于.net构架的留言板项目大全源码 http://down.51cto.com/z ...
- iOS 图片填充 UIImageView
UIViewContentModeScaleAspectFit, //这个图片都会在view里面显示,并且比例不变 这就是说 如果图片和view的比例不一样 就会有留白如下图1 UIView ...
- bzoj 2402: 陶陶的难题II 二分答案维护凸包
2402: 陶陶的难题II Time Limit: 40 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 68 Solved: 45[Submi ...
- HttpWebRequest提高效率之连接数,代理,自动跳转,gzip请求等设置问题
先设置4个: [csharp] webrequest.ServicePoint.Expect100Continue = false; //是否使用 Nagle 不使用 提高效率 webrequest. ...
- hdu 4739
一个超级超级水的题,不明白当时比赛的时候没有出来: 思路很简单,dfs暴力一下就行,枚举每个顶点,题目一共才20个点,就是20^4方的时间复杂度,完全可以承受: 代码: #include<cst ...
- ASP.NET多用户操作相同互斥的对象
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3574154.html ] 现象:公有静态变量不可用于ASP.NET多用户操作,否则该变量会被多 ...
- MST(prim)+树形dp-hdu-4756-Install Air Conditioning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4756 题目意思: n-1个宿舍,1个供电站,n个位置每两个位置都有边相连,其中有一条边不能连,求n个 ...
- String 类;Math 类;
static void Main(string[] args) { while (true) { /* strin ...