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> # ...
随机推荐
- cudaMemcpy2D介绍
cudaMemcpy2D( d_A, // 目的指针 d_pitch, // 目的pitch bmp1, // 源指针 sizeof(int)*2, // 源数据pitch sizeof(int)*2 ...
- 绕不开的this
犹豫两秒要不要整理this,从红皮书上看了半天,没搞懂哎(弱爆了) 什么是this?this是在执行上下文创建时期创建的一个执行过程中不可改变的变量.执行上下文是指js引擎会将代码执行前需要的变量th ...
- 前端css盒模型及标准文档流及浮动问题
1.盒模型 "box model"这一术语是用来设计和布局时使用,然后在网页中基本上都会显示一些方方正正的盒子.我们称为这种盒子叫盒模型. 盒模型有两种:标准模型和IE模型.这里重 ...
- github上更新fork项目
转载:https://blog.csdn.net/qq1332479771/article/details/56087333 ps:需要用GitHub所指定的chrome或者firefox浏览器,其它 ...
- 注释java中某个方法过时
添加一个注解即可 @Deprecated
- java数组之二分法查找
认识: 猜字游戏 步数 所猜的数 结果 可能值的范围 0 1~100 1 50 太高 1~49 2 25 太低 26~49 3 37 太高 26~36 4 31 太低 32~36 5 34 太 ...
- JAVA / MySql 编程——第五章 事务、视图、索引、备份和恢复
1.事务(Transaction): 事务是将一系列数据操作绑成一个整体进行统一管理. 如果一事务执行成功,则咋子该事务中进行的所有数据更改均会提交,称为数据库中的永久成部分. 如果事务执行是遇到错误 ...
- php mysql find_in_set函数 检索单子段 逗号分隔序列
FIND_IN_SET($kwd,field) 例如在 表 AA中 numbers 字段 保存列数据 1,4,8,75,41,7 就可以使用 FIND_IN_SET(8,numbers) 查询记 ...
- 详解JavaScript中的arc的方法
今天说说JavaScript在网页中画圆的函数arc! 一.arc所需要的参数设置 1 arc(x, y, radius, startAngle, endAngle, counterclockwise ...
- C语言进阶——关于07中指针的补充
首先我们应该了解指针可以分为: 野指针: 野指针不是NULL指针,是未初始化或未清零的指针,他指向的内存地址不是程序员想要的.人们一般不会错用NULL指针,因为用if语句很容易判断.但是“野指针”是很 ...