Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9588    Accepted Submission(s): 4480

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
 
Author
foreverlin@HNU
题意:
求长度为n的字符串中每个前缀在这条字符串中出现的次数的总和

代码:

//dp[i]表示字符串s[1~i]中出现了多少个以s[i]为结尾的前缀,又有f数组的特性,如果
//f[i]=j则s[1~j]=s[i-j+1~i]得出状态转移方程dp[i]=dp[f[i]]+1;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod=;
char P[];
int f[],dp[];
int main()
{
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
scanf("%s",P);
f[]=f[]=;
for(int i=;i<n;i++){
int j=f[i];
while(j&&P[i]!=P[j]) j=f[j];
f[i+]=(P[i]==P[j]?j+:);
}
dp[]=;
int sum=;
for(int i=;i<=n;i++){
dp[i]=dp[f[i]]+;
sum+=dp[i]%mod;
sum%=mod;
}
printf("%d\n",sum);
}
return ;
}

HDU3336 KMP+DP的更多相关文章

  1. hdu3336 KMP + DP 前缀数组出现的次数

    题意:       给你一个串,问你他的所有前缀子串在本串中的出现次数,注释:abc的前缀子串是 a ab abc; 思路:      还是利用了next数组,先对子串求出next数组,再开一个数组d ...

  2. [HDOJ5763]Another Meaning(KMP, DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...

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

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

  4. 【KMP+DP】Count the string

    KMP算法的综合练习 DP很久没写搞了半天才明白.本题结合Next[]的意义以及动态规划考察对KMP算法的掌握. Problem Description It is well known that A ...

  5. codeforces432D Prefixes and Suffixes(kmp+dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a strin ...

  6. [kmp+dp] hdu 4628 Pieces

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 Reincarnation Time Limit: 6000/3000 MS (Java/Ot ...

  7. 洛谷P3193 [HNOI2008]GT考试 kmp+dp

    正解:kmp+dp+矩阵优化 解题报告: 传送门! 啊刚说想做矩阵优化dp的字符串题就找到辣QwQ虽然不是AC自动机的但都差不多嘛QwQ 首先显然可以想到一个dp式?就f[i][j]:凑出i位了,在s ...

  8. [BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂)

    [BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂) 题面 阿申准备报名参加GT考试,准考证号为N位数X1X2-.Xn,他不希望准考证号上出现不吉利的数字.他的不吉利数学A ...

  9. HDU 6153 A Secret ( KMP&&DP || 拓展KMP )

    题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...

随机推荐

  1. Linux系统inotify工具安装配置

    inotify主要功能 Inotify 是一个 Linux特性,它监控文件系统操作,比如读取.写入和创建.Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多.学习如何将 ...

  2. 3.azkaban3.0测试

    测试目标 azkaban多executor下flow的分配方式 azkaban可以同时执行的flow\job个数 azkaban单个job最小使用的内存 相关配置 executor最大线程数: exe ...

  3. 今年暑假不AC (贪心)

    Description “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会 ...

  4. UVA725 Division (暴力求解法入门)

    uva 725 Division Write a program that finds and displays all pairs of 5-digit numbers that between t ...

  5. .net下将exe改造为既能双击独立运行又能注册为windows服务

    最近项目中需要将一些业务的处理程序改造为windows服务,但是考虑到实际需求,也需要能够直接双击运行这些处理程序.首先第一步想到的就是原来的项目不变,只需要在加一个windows服务的项目就行.但是 ...

  6. ssl证书验证的问题

    对于https请求,是需要ssl证书验证的请求的,所以如果在请求时如果不带ssl证书,那么可以忽略证书的验证 有三种方法去实现: 1.Requests请求: 在文档中可以看到:http://docs. ...

  7. 微信小程序项目笔记以及openId体验版获取问题

    公司一直说要搞小程序,说了几个月,最近才算落地,一个很小的项目,就结果来讲,勉强让自己窥得小程序门径. 下面总结一下,为了弄好小程序,所学到的知识,以及项目中遇到的问题以及解决的办法.纯属个人见解. ...

  8. 图解linux安装tomcat(附常用命令)

    本例使用的是centos6.5版本,具体内容如下 一.首先到官方下载tomcat服务 http://tomcat.apache.org/download-70.cgi 二.将tomcat上传至linu ...

  9. TCP/IP三次握手与四次握手

    原文地址 http://blog.csdn.net/whuslei/article/details/6667471 http://blog.csdn.net/wo2niliye/article/det ...

  10. Spring Boot中使用@Transactional注解配置事务管理

    事务管理是应用系统开发中必不可少的一部分.Spring 为事务管理提供了丰富的功能支持.Spring 事务管理分为编程式和声明式的两种方式.编程式事务指的是通过编码方式实现事务:声明式事务基于 AOP ...