dp[i]代表前i个字符组成的串中所有前缀出现的次数。

dp[i] = dp[next[i]] + 1;

因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-next[i]+1 ]~str[len],即串的前缀后缀中最长的公共长度。

对于串ababa,所有前缀为:a, ab,aba,abab, ababa, dp[3] = 3;

到达dp[5]的时候,next = 3, 它与前面的最长公共前缀为aba,因此dp[5]的凑法应该加上dp[3],再+1是加上ababa它本身。

说不太清楚……从感觉上比较类似于那个“给你几种面值的硬币,问凑出指定钱数的所有凑法”的dp方式。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int MAXN = ;
const int MOD = ; int len;
char str[MAXN];
int nextval[MAXN];
int dp[MAXN]; void getNext(char s[],int next[])
{
int length=len;
int i=,j=-;
next[]=-;
while(i<length)
{
if(j==-||s[i]==s[j])
{
++i;
++j;
next[i]=j;
}
else
j=next[j];
}
} int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%d", &len );
scanf( "%s", str ); getNext( str, nextval ); int ans = ;
memset( dp, , sizeof(dp) );
for ( int i = ; i <= len; ++i )
{
dp[i] += ( dp[ nextval[i] ] + )%MOD;
ans += dp[i];
ans %= MOD;
}
printf( "%d\n", ans % MOD );
}
return ;
}

HDU 3336 Count the string ( KMP next函数的应用 + 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

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

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

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

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

  6. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 3336 Count the string(next数组运用)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdu 3336 Count the string(思维可水过,KMP)

    题目 以下不是KMP算法—— 以下是kiki告诉我的方法,好厉害的思维—— 就是巧用标记,先标记第一个出现的所有位置,然后一遍遍从标记的位置往下找. #include<stdio.h> # ...

随机推荐

  1. 中期ppt制作

    陀螺仪的使用解释:https://zhuanlan.zhihu.com/p/29244429 手机坐标轴的图片:http://jcjs.siat.ac.cn/ch/reader/create_pdf. ...

  2. 简单使用hibernate(idea中使用)

    首先创建一个maven项目 创建成功后,进行创建数据库的表 CREATE TABLE BOOK( ID INT AUTO_INCREMENT PRIMARY KEY, NAME ), NUMBER i ...

  3. Spring初始化Bean或销毁Bean前执行操作的方式

    如果想在Spring初始化后,或者销毁前做某些操作,常用的设定方式有三种: 第一种:通过 在xml中定义init-method 和 destory-method方法 推荐使用,缺陷是只能在XML中使用 ...

  4. HTML第二章:列表,表格,媒体元素

    第二章:列表,表格,媒体元素 列表:有三种,有序列表,无序列表,定义列表 1.有序列表:<ol></ol>            列表项:<li></li&g ...

  5. JavaSE 面试题总结

    一. JavaSE 4 1. 面向对象的特征有哪些方面 4 2. String是最基本的数据类型吗? 4 3. super()与this()的区别? 4 4. JAVA的事件委托机制和垃圾回收机制 4 ...

  6. top小火箭

    // my.js function $(id){return document.getElementById(id)};function show(obj){obj.style.display = & ...

  7. 字符串替换For linux C

    1.临时空间给了个1024,不需要可减少长度. 2.结果只用用strcpy了,没校验. bool Replace(char *str,const char *src, const char *des) ...

  8. 浅谈mysql权限

    一.      背景: “去IOE”的本质是“分布式+开源”架构替代“集中式+封闭”架构,变成彻底的云计算服务模式.去“IE”易,并且应该去,关键确实能省钱,而且运维难度不大,替代技术产品成熟.而去O ...

  9. Spring框架基础2

    Spring框架基础2 测试Spring的AOP思想和注解的使用 导包(在前面的基础上添加) SpringAOP名词解释 AOP编程思想:横向重复代码,纵向抽取:就是说多个地方重复的代码可以抽取出来公 ...

  10. ofbiz研究

    近段时间,刚有有时间研究了下ofbiz ; 目前还是刚开始,后期会记录过程 有一起研究的没