POJ 3336 Count the string (KMP+DP,好题)
参考连接:
KMP+DP:
http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html
另外给出一个没用dp做的:
http://blog.sina.com.cn/s/blog_82061db90100usxw.html
题意:
给出一个字符串,求它的各个前缀在字符串中出现的次数总和。
思路:
记 dp[i] 为前 i 个字符组成的前缀出现的次数
则 dp[next[i]]+=dp[i]
dp[i]表示长度为i的前缀出现的次数,初始条件dp[i]=1,即至少出现一次。
举例:
index:012345
ababa
next: 000123
dp[5]=1;
dp[4]=1;
i=5:dp[3]=dp[3]+dp[5]=2;
i=4:dp[2]=dp[2]+dp[4]=2;
i=3:dp[1]=dp[1]+dp[3]=3;
即各前缀出现次数:
a:3
ab:2
aba:2
abab:1
ababa:1
至于如何理解状态方程dp[next[i]]+=dp[i],看下面那张图:

设前缀s长度为i,在字符串中出现的次数为3次,即为图中的s1,s2,s3。
图中红色部分即为前缀与后缀相同的子串,长度为next[i]。
设为p1,p2,p3,p4(其中p2,p3各为两次重叠)
可以知道,p1即为一开始初始化时算入进去的1,而p2,p3,p4正好对应s1,s2,s3,即s在字符串中出现的个数dp[i]。
这样状态转移方程就好理解了。
dp[next[i]]=dp[next[i]]+dp[i]。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <string.h> using namespace std;
const int maxn=;
const int mod=;
int n;
char str[maxn];
int next[maxn];
int dp[maxn]; //dp[i]表示长度为i的前缀出现的次数
int len,L,l,num;
void getNext(char*str){
int k=,lm=strlen(str);
next[]=;
for(int i=;i<lm;i++){
while(k> && str[k]!=str[i])
k=next[k];
if(str[k]==str[i])
k++;
next[i+]=k;
}
}
int main()
{
int t;
char tmp[maxn];
scanf("%d",&t);
while(t--){
scanf("%d",&len);
scanf("%s",str);
getNext(str);
for(int i=;i<=len;i++)
dp[i]=; //初始均为1
for(int i=len;i>=;i--){
dp[next[i]]=(dp[next[i]]+dp[i])%mod;
}
int ans=;
for(int i=;i<=len;i++)
ans=(ans+dp[i])%mod; printf("%d\n",ans);
}
return ;
}
POJ 3336 Count the string (KMP+DP,好题)的更多相关文章
- 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 -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][DP]
题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...
- 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 ...
- hdu3336 Count the string kmp+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- hud 3336 count the string (KMP)
这道题本来想对了,可是因为hdu对pascal语言的限制是我认为自己想错了,结果一看题解发现自己对了…… 题意:给以字符串 计算出以前i个字符为前缀的字符中 在主串中出现的次数和 如: num(aba ...
- Count the string (KMP+DP)
题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { , ...
- 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) ...
随机推荐
- EMVTag系列3《持卡人基本信息数据》
Ø 9F61 持卡人证件号 L:2–26 R(需求):数据应存在,在读应用数据过程中,终端不检查: (PBOC2.0第五部分中规定)芯片中持卡人姓名 5F20与持卡人姓名扩展9F0B只能使用一 ...
- EMVTag系列6《IAC 发卡行行为代码》
R(需求):数据应存在,在读应用数据过程中,终端不检查:将变成必备 L: 5 IAC 的值,最好不要自定义,最好和所选的模板完全匹配.如果修改持卡人认证位,PIN Bypass 相关的位.如果有业务需 ...
- linux系统设置-防火墙
基础知识 Linux系统内核内建了netfilter防火墙机制.Netfilter(数据包过滤机制),所谓的数据包过滤,就是分析进入主机的网络数据包,将数据包的头部数据提取出来进行分析,以决该连接为放 ...
- esp和ebp详解
最近在研究栈帧的结构,但总是有点乱,所以写了一个小程序来看看esp和ebp在栈帧中的作用.这个程序如下: 这个程序很简单,就是求两个数的值,然后输出即可.所以首先把它用gcc编译链接成a.out,进入 ...
- 【Javascript】: for循环中定义的变量在for循环体外也有效
for循环中定义的变量在for循环体外也有效 <script> (function(){ var a = 111; for(var i=0;i<5;i++){ var carl = ...
- GNU make 总结 (五)
一.使用make更新静态库 静态库文件是一些.o文件的集合,在Linux中使用ar工具对它进行维护管理.一个静态库通常由多个.o文件组成,这些.o文件可独立的被作为一个规则的目标,库成员作为目标时需要 ...
- Call C# in powershell
How to call C# code in powershell Powershell Command Add-Type usage of Add-Type we use Add-Type -Typ ...
- 63.Assignment to name ignored, since the identifier is never used
在文件中单独定义一些信号变量,如start_flag,即使进行(*KEEP=“TRUE”*)的声明,但在cdc文件上依然找不到start_flag变量,这是为什么呢?因为ISE综合器非常聪明,对一些没 ...
- Linux Shel高级技巧(目录)
Linux Shell高级技巧(一) http://www.cnblogs.com/stephen-liu74/archive/2011/12/22/2271167.html一.将输入信息转换为大写字 ...
- sql优化点整理
此文是我最早开始sql优化至今整理的小知识点和经常遇到的问题,弄懂这些对优化大型的sql会有不少帮助 ---------------------------------使用了多余的外连接------- ...