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-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 )的更多相关文章
- 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
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- [HDU 3336]Count the String[kmp][DP]
题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...
- 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 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336 Count the string(思维可水过,KMP)
题目 以下不是KMP算法—— 以下是kiki告诉我的方法,好厉害的思维—— 就是巧用标记,先标记第一个出现的所有位置,然后一遍遍从标记的位置往下找. #include<stdio.h> # ...
随机推荐
- 如何在Linux中显示和设置主机名
原文链接 随着连接到网络的计算机数量越来越多,每一台计算机都需要有一个属性来区别于其它计算机.和现实世界中的人一样,计算机也有一个叫做hostname(主机名)的属性. 什么是hostname 从它的 ...
- js延迟执行与循环执行
延迟一段时间执行特定代码: setTimeout(function () { window.location.href = 'login' },1200); 循环执行: function test() ...
- TryParse()的用法
DateTime dt = new DateTime(); DateTime.TryParse(txtName.text.trim(),out dt); string str1 = dt.ToStri ...
- STL 之 set的应用
关于set Set是STL中的一个容器,特点是其中包含的元素值是唯一的,set根据其底层实现机制分为hash存储和红黑树存储两种方式,这两种结构最本质的区别就是有序和无序,红黑树的存储是有序的而has ...
- poj_3641_Pseudoprime numbers
Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). Th ...
- 手机丢了怎么办?MZ给你来支招
1致电运营商挂失手机 2致电银行冻结手机网银 3手机绑定支付宝的拨95188挂失 4微信用户登录110.qq.com冻结账号 5修改微博.微信.QQ等密码 6到手机运营商处补手机卡. 一定要记住啊!手 ...
- scrapy--boss直聘
Hi,大家好.有段时间没来更新scrapy爬取实例信息了,前2天同事说爬取拉勾,boss直聘等网站信息比较困难.昨天下午开始着手爬取boss直聘内Python爬虫的信息,比想象中的简单很多. 需要解决 ...
- Myeclipse上配置weblogic11g(10.3.6)的方法教程
1.在windows-perferences-MyEclipse-Servers-weblogic下找到weblogic10.X 2.按照参考我上面的附图填入weblogic的相关路径(如果你是完全按 ...
- 使用file_get_contents()和curl()抓取网络资源的效率对比
使用file_get_contents()和curl()抓取网络资源的效率对比 在将小程序用户头像合成海报的时候,用到了抓取用户头像对应的网络资源,那么抓取方式有很多,比如 file_get_cont ...
- arm-none-linux-gnueabi-gcc No such file or directory这个错误的解决方法
这个gcc可执行文件是32位的版本,而在64位系统上需要安装32位兼容包才可以运行正常 .用file命令查看这个文件得到: 解决办法: 安装ia32-libs sudo apt-get install ...