参考连接:

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. sublimeLinter-jshint 配置

    这几天知道sublime3有可以对javascript进行语法检查的文件,折腾了一上午,搞定了. 记录一下步骤: 1.先安装nodejs. 2.npm install jshint -g 3.通过su ...

  2. Java 控制台执行带自定义包定义的类,出现“Exception in thread "main" java.lang.NoClassDefFoundError: ConnectSQLServer (wrong name: sine/ConnectSQLServer)”

    1.先说明一下代码实现:自定义package sine, 源代码保存路径为:E:\JSP\HibernateDemo\HibernateDemoProject\src\sine\ConnectSQLS ...

  3. VBA在WORD中给表格外的字体设置为标题

    使用VB可以将表外的字体设置标题字体实际操作如下: VB代码如下: Sub oliver_1() Selection.EndKey Unit:=wdStory '光标移到文末 To ActiveDoc ...

  4. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  5. c++基础(二):成员he派生类

    struct Date{ int day, month, year; }; struct Book{ string name; Date date; void init(string n, int y ...

  6. Swift global function(count indexOfObject contains...)

    当你在使用Swift时会发现一些常用的函数不!见!了! 比如 String:    s.count()  s.contains() Array: a.indexOfObeject(t:<T> ...

  7. 【转载】VGA时序与原理

    显示器扫描方式分为逐行扫描和隔行扫描:逐行扫描是扫描从屏幕左上角一点开始,从左像右逐点扫描,每扫描完一行,电子束回到屏幕的左边下一行的起始位置,在这期间,CRT对电子束进行消隐,每行结束时,用行同步信 ...

  8. C语言函数返回数组

    #include "stdio.h"/*int* set(int a,int *c){ int *b; b=malloc(sizeof(int)*3); c[0]=a; c[1]= ...

  9. 在VS2010中ActiveX控件注册方法,使用regsvr32命令

    上一篇小编展示了如何设置VS2010自带的ActiveX控件的容器测试程序,现在为大家演示一下如何注册ActiveX控件. 首先简单了解一下ActiveX控件的知识,ActiveX控件:简单来说,就是 ...

  10. loadView, viewDidLoad 快速使用

    一  loadView: 在每次访问 UIViewController时,且其 view = nil 时,会调用这个方法,所以大家在开发中想自己设置 view 的可以用这个方法,在这个方法中自定义 v ...