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

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.
 
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
#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算法求前缀在原字符串中出现总次数】的更多相关文章

  1. 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 ...

  2. hud 3336 count the string (KMP)

    这道题本来想对了,可是因为hdu对pascal语言的限制是我认为自己想错了,结果一看题解发现自己对了…… 题意:给以字符串 计算出以前i个字符为前缀的字符中 在主串中出现的次数和 如: num(aba ...

  3. [HDU 3336]Count the String[kmp][DP]

    题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...

  4. 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 ...

  5. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  6. 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 ...

  7. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. POJ 3336 Count the string (KMP+DP,好题)

    参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...

  9. ACM hdu 3336 Count the string

    [题意概述] 给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和. [题目分析] 利用kmp算法的next数组 再加上dp [存在疑惑] 在分析nex ...

随机推荐

  1. centos 基本操作(输入法安装,adls宽带连接,查找文件,模拟终端)

    centos 基本操作之输入法安装输入法启动终端,切换到超级管理员yum install "@Chinese Support"安装成功后,退出System->Preferen ...

  2. 对React的理解

    转自:http://www.cocoachina.com/webapp/20150721/12692.html 现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了R ...

  3. php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event

    php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event

  4. 根据日期自增的sql语句

    Insert into wd_orderitem (count , id_dish , state , info , sn , id_order)values(1 , 1000000001 , 3 , ...

  5. lnmp安装--php与nginx结合

    软件环境: linux:centos5. nginx:.tar.gz php:.tar.gz lnmp与lamp的区别? lnmp(linux+nginx+mysql+php)的提法相对于lamp(l ...

  6. iOS 常用基础框架

    框架名称 功能 Foundation 提供OC的基础类(像NSObject).基本数据类型等 UIKit 创建和管理应用程序的用户界面 QuartzCore 提供动画特效以及通过硬件进行渲染的能力 C ...

  7. Java编程规范整理

    分享一份网友整理的编程过程中的命名规范 包命名 包名按照域名的范围从大到小逐步列出,恰好和Internet上的域名命名规则相反. 由一组以"."连接的标识符构成,通常第一个标识符为 ...

  8. 浅析PC机串口通讯流控制

    转自浅析PC机串口通讯流控制 我们在串行通讯处理中,常常看到RTS/CTS和XON/XOFF这两个选项,这就是两个流控制的选项,目前流控制主要应用于调制解调器的数据通讯中,但对普通RS232编程,了解 ...

  9. qt 原子操作 QAtomicInt(++和--都不是原子操作:都包含三条机器指令)

    ++和--都不是原子操作:都包含三条机器指令 http://sroply.blog.163.com/blog/static/17092651920106117251539/

  10. GCC优化选项-fomit-frame-pointer对于esp和ebp优化的作用

    我的博客:www.while0.com -fomit-frame-pointer选项是发布产品时经常会用到的优化选项,它可以优化汇编函数中用edp协助获取堆栈中函数参数的部分,不使用edp,而是通过计 ...