[KMP][HDU3336][Count the string]
题意
计算所有S的前缀在S中出现了几次
思路
跟前缀有关的题目可以多多考虑KMP的NEXT数组
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
char S[2000000];
int NEXT[2000000];
int dp[2000000];//dp[i] 表示 以i结尾的子串与前缀相等的个数 d[i]=d[next[i]]+1;
//一开始还以为是 d[i]=next[i]+1; 在abab这个样例中 d[3]=3 显然有重复计算了
int len;
int sum=0;
void get_next()
{
for(int i=1;i<=len;i++)
{
int p=i-1;
while(S[i]!=S[NEXT[p]+1]&&p!=0) p=NEXT[p];
if(p!=0) NEXT[i]=NEXT[p]+1; //这种不else 的写法注意清空NEXT
//else NEXT[i]=0;
}
}
int main()
{
// freopen("a.in","r",stdin);
int T;
cin>>T;
while(T--)
{
sum=0;
cin>>len;
memset(NEXT,0,sizeof(NEXT));
scanf("%s",S+1);
get_next();
for(int i=1;i<=len;i++)
{
dp[i]=dp[NEXT[i]]+1;
sum=(sum+dp[i])%10007;
}
cout<<sum<<endl;
}
}
[KMP][HDU3336][Count the string]的更多相关文章
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
- hdu3336 Count the string 扩展KMP
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- hdu3336 Count the string kmp+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...
- HDU3336 Count the string KMP 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...
- HDU3336 Count the string(kmp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- HDU3336 Count the string 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...
- HDU3336 Count the string
居然一A了,说明对朴素的KMP还是有一定理解. 主要就是要知道next数组的作用,然后就可以计算每个i结尾的满足题意的串个数. #include<cstdio> #include<c ...
- 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 ...
随机推荐
- HTTP协议报文格式
HTTP协议报文格式 接下来我们看看HTTP协议(Hypertext Transfer Protocol――超文本传输协议)浏览器端(客户端)向WEB服务器端访问页面的过程和HTTP协议报文的格式. ...
- NET中级课--文件,流,序列化1
1.对于机器的角度来看,任何文件都是二进制的0和1. 2. 位:bit,一个1或0就是1位. 字节:byte,每8位一个字节.一个字节的范围就是00000000到1111111,换成10进制就是0 ...
- 解决Xcode6.4安装插件后插件不能使用的问题
下面是上网查的方法,综合了一下,亲测 原因: 苹果要求加入UUID证书从而保证插件的稳定性. 解决方法: 一.查看Xcode的UUID 在终端执行 defaults read /Application ...
- html进阶css(5)
css定位机制 css有三种基本的定位机制:普通流,浮动和绝对定位. 除非专门指定,否则所有框都在普通流中定位.也就是说,普通流中的元素的位置由元素在html中的位置决定. 块级框从上到下一个接一个的 ...
- Git(Repo)常用命令收集
(注意: 只记录工作中实际使用的命令) 同步android源码 repo sync:(可加-c,只取当前分支: 可加-j4,线程数量) 查看android源码下所有项目的git状态 rep ...
- SQL日期形式转换
在SQL Server中,有时存储在数据库中的日期格式和我们需要显示在页面上的格式不相同,我们需要转化成需要的格式. 特在此总结了一下常用的日期格式. --当前时间 SELECT GETDATE(); ...
- UIDeviceOrientation UIInterfaceOrientation 区别
UIDeviceOrientation 是机器硬件的当前旋转方向 这个你只能取值 不能设置 UIInterfaceOrientation 是你程序界面的当前旋转方向 这个可以设置 ...
- java集合使用——HashMap
在map中插入.删除和定位元素时,HashMap是最好的选择.如果要按照自然顺序或自定义顺序遍历(获取所有元素),那么treemap更好一些. 第一:构造和添加元素 HashMap map = new ...
- x^2+y^2=N的整数解?
本文系转载:http://blog.sina.com.cn/s/blog_a661ecd50101cv41.html 我们先研究这个问题的一部分:哪些素数是两平方数之和?为什么我们先研究素数,有个很重 ...
- (原)Vs中debug和release结果不一致
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5598091.html 前两天调试程序,出现了一个很蛋疼的问题,debug和release结果不一致.网 ...