【HDU 3336 Count the string】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11607 Accepted Submission(s): 5413
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
Source
HDOJ Monthly Contest – 2010.03.06
Recommend
lcy
题解:
①首先发现如果长度为i前缀出现了,那么长度小于i的前缀也就随之出现了至少一次。
②考虑KMP中的next数组表示了最长前后缀的性质,可以使用如下DP:
num[i]表示序列a[0~i]中出现了好多个前缀(不论长度)
转移方程式:num[i]=num[next[i]]+1 (+1是自己匹配自己)
③这样做的原因:next指向最长前缀,那么相同的当前后缀可以再构造一模一样的解。
④至此发现其实num[i]的定义是有缺陷的,只是为了方便理解。因为为了不重复计算答案,必须不能保存以前的答案(即答案不是num[m])。
#define M 10007
#include<stdio.h>
#define go(i,a,b) for(int i=a;i<=b;i++)
const int N=200003;int T,m,j,f[N],num[N],ans;char P[N];
int main()
{
scanf("%d",&T);
while(ans=0,T--&&scanf("%d%s",&m,P))
{
go(i,1,m-1){j=f[i];while(j&&P[i]!=P[j])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}
go(i,1,m)(ans+=((num[i]=num[f[i]]+1)%=M))%=M;
printf("%d\n",(ans%M+M)%M);
}
return 0;
}//Paul_Guderian
Turns out,real life's a little bit more complicated than a slogan on a bumper sticker……
————Judy·Hopps
【HDU 3336 Count the string】的更多相关文章
- HDU 3336 Count the string(KMP的Next数组应用+DP)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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代码: # ...
- ACM hdu 3336 Count the string
[题意概述] 给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和. [题目分析] 利用kmp算法的next数组 再加上dp [存在疑惑] 在分析nex ...
- HDU 3336——Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
随机推荐
- MySQL工作经验
以下是根据工作中遇到各种场景用到的一些Mysql用法,比较实用,基本是语法之外的一些东西. 修改账户密码 1.打开Mysql控制台,输入原密码: 2.输入以下语法:mysql> set pass ...
- 爬虫学习(十三)——xpath基础学习
lxml的作用 lxml是HTML.xml的解析器,主要的功能是如何解析和提取HTML和xml数据 lxml和正则一样,也是使用C来实现的,是一款高性能的python HTML/xml解析器,我们可以 ...
- mysql架构和历史
存储引擎 查看: show table status like 'bigcourse'; 结果: +-----------+--------+---------+------------+------ ...
- (二)、Python 基础
Python入门 一.第一句Python 在 /home/dev/ 目录下创建 hello.py 文件,内容如下: print "hello,world" 执行 hello.py ...
- tar工具(打包,压缩)
tar工具(打包,压缩)========================= tar打包工具 -c:表示建立一个tar包或者压缩文件包-x:表示解包或者解压缩-v:表示可视化-f: 后面跟文件名(即-f ...
- JAVAOOP异常
排序: Try-catch-finally:try正常执行,如果有异常执行catch后执行finally,如果没有直接执行finally 执行顺序:try-catch:try中的语句正常执行,如果遇到 ...
- 获取点击li的当前索引
获取点击li的当前索引 点击特定次序的li 展现特定的页面 $('.wgsb').find('.wangge_data_list li').click(function(){ var index=$ ...
- python中的列表内置方法小结
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- ''' names=['zhangyu','mahongyan','zhangguobin','shac ...
- pycharm中文乱码问题 总结
前言: 这几天刚刚开始学习python,然后就安装了pycharm,但是那个中文乱码的问题真是让人心烦,在网上找了好久,都写得好乱,今天终于让我解决了,在这里总结一下经验,希望可以帮到你们 问题:如下 ...
- python-7面向对象高级编程
1-给类动态增加方法 class Student(object): pass def set_score(self, score): self.score = score Student.set_sc ...