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) ...
随机推荐
- hdu 2578 Dating with girls(1)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2578 Dating with girls(1) Description Everyone in the ...
- Apple Watch应用开发经验谈:我遇到的那些坑
本文作者张忠良是滴答清单Apple Watch版应用的开发工程师,他用了一周的时间使用纯Objective-C语言完成了Apple Watch版滴答清单应用的开发工作.在这里,他从开发角度阐述了个人对 ...
- 使用 PHP cURL 提交 JSON 数据
http://www.oschina.net/code/snippet_54100_7351 http://www.lornajane.net/posts/2011/posting-json-data ...
- Linux rsync 同步
rsync 是一个快速增量文件传输工具,它可以用于在同一主机备份内部的备分,我们还可以把它作为不同主机网络备份工具之用.本文主要讲述的是如何自架rsync服务器,以实现文件传输.备份和镜像.相对tar ...
- Entity Framework 学习第一天 续
改写第一天的增删改查方法,观察增删改查的本质 using System; using System.Collections.Generic; using System.Data.Entity.Infr ...
- qt 焦点设置策略
focusPolicy 一个QWidget获得焦点的方式受 focusPolicy 控制 Qt::TabFocus 通过Tab键获得焦点 Qt::ClickFocus 通过被单击获得焦点 Qt::St ...
- C++设计模式——享元模式
本文版权归果冻说所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利.如果这篇文章对你有帮助,你可以请我喝杯咖啡. » 本文链接:http:// ...
- nodeJs事件之监听移除事件
var EventEmitter=require('events').EventEmitter var life=new EventEmitter(); //comfort 求安慰,函数名: //fo ...
- HashMap优雅的初始化方式以及引申
小记 相信很多人和笔者一样,经常会做一些数组的初始化工作,也肯定会经常用到集合类.假如我现在要初始化一个String类型的数组,可以很方便的使用如下代码: String [] strs = {&quo ...
- UVALive - 6572 Shopping Malls floyd
题目链接: http://acm.hust.edu.cn/vjudge/problem/48416 Shopping Malls Time Limit: 3000MS 问题描述 We want to ...