[HDU 3336]Count the String[kmp][DP]
题意:
求一个字符串的所有前缀串的匹配次数之和.
思路:
首先仔细思考: 前缀串匹配.
n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串.
对于一个前缀串, 我们需要计算它的匹配次数.
k = next [ j ]
表示前缀串 Sj 的范围内(可以视为较小规模的子问题), 前缀串 Sk 是最长的&能够匹配两次的前缀串.
这和我们需要的答案有什么关系呢?
题目是求所有前缀串的匹配次数之和, 那么可以先求前缀串 Si 在整个串中的匹配次数, 再加和.
到此, 用到了两个"分治", 一是将大规模的问题减小为小规模的问题, 二是将询问的最终结果拆分成一个个步骤, 则专注于分析核心步骤.
可设dp[ i ]为前缀串 Si 在总串中出现的次数.
dp[i] = 1;
dp[next[i]] += dp[i];
#include <cstring>
#include <cstdio>
const int MAXN = 200005;
const int MOD = 10007;
int dp[MAXN],next[MAXN];
char s[MAXN];
//46MS 1960K
void prekmp()
{
next[0] = -1;
int j = -1;
for(int i=1;s[i];i++)
{
while(j!=-1 && s[j+1]!=s[i]) j = next[j];
if(s[j+1]==s[i]) j++;
next[i] = j;
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
scanf("%s",s);
prekmp();
for(int i=0;i<n;i++) dp[i] = 1;
dp[n] = 0;
for(int i=n-1;i;i--)
{
if(next[i]!=-1) {dp[next[i]] += dp[i];dp[next[i]] %= MOD;}
}
for(int i=0;i<n;i++) {dp[n] += dp[i];dp[n] %= MOD;}
printf("%d\n",dp[n]);
}
}
还有另一种思路:
可以将前缀的匹配次数视为包含的前缀个数.
最终的问题是求s中 (设每一种前缀i包含的前缀个数Fi ) ΣFi .
dp[i]为前缀串s[0...i]包含的前缀个数的新增数目(相对于前缀串s[0...i-1]).
则
dp[i] = dp[next[i]] + 1;//1表示它本身也是新增的一个前缀
#include <cstring>
#include <cstdio>
const int MAXN = 200005;
const int MOD = 10007;
int dp[MAXN],next[MAXN];
char s[MAXN];
//62MS 1960K
void prekmp()
{
next[0] = -1;
int j = -1;
for(int i=1;s[i];i++)
{
while(j!=-1 && s[j+1]!=s[i]) j = next[j];
if(s[j+1]==s[i]) j++;
next[i] = j;
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
scanf("%s",s);
prekmp();
memset(dp,0,sizeof(int)*(n+1));
for(int i=0;i<n;i++)
{
if(next[i]!=-1)
{
dp[i] = dp[next[i]]+1;
dp[i] %= MOD;
}
else
dp[i] = 1;
}
for(int i=0;i<n;i++) {dp[n] += dp[i];dp[n] %= MOD;}
printf("%d\n",dp[n]);
} }
[HDU 3336]Count the String[kmp][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 next函数的应用 + DP )
dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- 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(next数组运用)
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(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
随机推荐
- hive join 优化
common join : 即reducer join,瓶颈在shuffle阶段,会产生较大的网络io: map join:即把小表放前面,扫描后放入每个节点的内存,在map阶段进行匹配: 开启map ...
- [原创] 小而美 | Mac上鲜为人知,但极大提升效率的小工具
热爱收集实用又好用的软件,工具类软件不在多,发挥作用,提高效率最重要~推荐几个压箱底的藏货 一.Noizio -自然而然的白噪声,专注工作 Noizio是一款OS X 下的白噪音应用,可以让自己觉着是 ...
- 彻底解决 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
最近我的VS2010不知道怎么回事,平时用的好好的,近期竟然出现了所谓的 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 头痛万分,查了各种资料一 ...
- Scala学习之延迟绑定
package com.swust.example object TraitDemo2 extends App{ //抽象类 abstract class Writer { def writeMess ...
- 几年前无聊小游戏之作_WEB版本打泡泡
几年前写的小东西 主要是H5画布的操作,还有个C语言基于WIN SDK开发的版本 找不到代码了 找到了再分享 <!DOCTYPE html> <script src="ga ...
- android 布局常用混淆属性
1.如何控制某一控件在父控件中的相对位置呢? 在Android系统中提供了layout_margin,用来控制某一控件边缘相对于父控件的边距. 其中, android:layout_marginTop ...
- angular checkbox required
Here is the fiddle http://jsfiddle.net/lumixraku/xgLz7d4j/2/ html <body ng-app="app"> ...
- DNS解析
大家好,今天51开源给大家介绍一个在配置文件,那就是/etc/resolv.conf.很多网友对此文件的用处不太了解.其实并不复杂,它是DNS客户机配置文件,用于设置DNS服务器的IP地址及DNS域名 ...
- Windows 系统消息范围和前缀,以及消息大全
Windows系统定义的消息类别消息标识符前缀 消息分类ABM 应用桌面工具栏消息BM 按钮控件消息CB 组合框控件消息CBEM 扩展组合框控件消息CDM 通用对话框消息DBT 设备消息DL 拖曳列表 ...
- HDU 4983 Goffi and GCD
题目大意:给你N和K,问有多少个数对满足gcd(N-A,N)*gcd(N-B,N)=N^K.题解:由于 gcd(a, N) <= N,于是 K>2 都是无解,K=2 只有一个解 A=B=N ...