参考连接:

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,好题)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [HDU 3336]Count the String[kmp][DP]

    题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...

  4. 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 ...

  5. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...

  6. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  7. hud 3336 count the string (KMP)

    这道题本来想对了,可是因为hdu对pascal语言的限制是我认为自己想错了,结果一看题解发现自己对了…… 题意:给以字符串 计算出以前i个字符为前缀的字符中 在主串中出现的次数和 如: num(aba ...

  8. Count the string (KMP+DP)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { , ...

  9. 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) ...

随机推荐

  1. Python 3.5.2建立与DB2的连接

    Python是可以连接数据库,并从数据库获取相应的数据库的,但是怎么连接呢? 这是个问题,以下是我使用Python建立数据库连接的步骤(我使用的工具为:PyCharm) 1.首先下载setuptool ...

  2. 在Java如何保证方法是线程安全的

    废话开篇 都说Java程序好些,但是我觉得Java编程这东西,没个十年八年的真不敢说自己精通Java编程,由于工作原因,开始转战Java多线程,以前没怎么接触过,所以想留点脚印在这两条路上. 切入正题 ...

  3. Android内存管理机制

    相信一步步走过来的Android从业者,每个人都会遇到OOM的情况.如何避免和防范OOM的出现,对于每一个程序员来说确实是一门必不可少的能力. 今天我们就谈谈在Android平台下内存的管理之道,开始 ...

  4. iOS之走进精益编程01

    Model类 .h #import <Foundation/Foundation.h> @interface Product : NSObject @property (nonatomic ...

  5. C#判断字符串为空

    string str = null; if (string.IsNullOrWhiteSpace(str)) { MessageBox.Show("字符串为null"); } if ...

  6. MVC Razor模板引擎输出HTML或者生产HTML文件

    以前做CMS的时候都会根据模板来生成输出HTML或者生成HTML文件. 常用的引擎有VTemplate.NVelocity等等,这个我就布做介绍了. 这里我想说的是.当mvc出现Razor模板引擎的时 ...

  7. python time,string 转换

    1. 将字符串转换成时间,字符串格式为05/16/2015 datetime.datetime.strptime(STRING,"%m/%d/%Y") 2. 将时间转换成字符串:格 ...

  8. ATR与ATS

    ATR:answer to reset  复位应答 ATS:answer to select 选择应答

  9. “我爱淘”冲刺阶段Scrum站立会议7

    完成任务: 大事不好,今天的任务还没有完成,没有通过xml文件通过服务器显示到软件中. 计划任务: 实现通过服务器将xml文件中的数据显示到软件中. 遇到问题: 服务器已经配好,并且解析xml文件的代 ...

  10. EasyUI 在aspx页面显示高度不正常解决办法

    <body class="easyui-layout"> <form id="form1" runat="server"& ...