[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[ ...
随机推荐
- BZOJ 1935: [Shoi2007]Tree 园丁的烦恼( 差分 + 离散化 + 树状数组 )
假如矩阵范围小一点就可以直接用二维树状数组维护. 这道题, 差分答案, 然后一维排序, 另一维离散化然后树状数组维护就OK了. ----------------------------------- ...
- FileInputStream(字节流)与fileReader(字符流) 的区别
FileInputStream 类 1 ) FileInputStream 类介绍: 以字节为单位的流处理.字节序列:二进制数据.与编码无关,不存在乱码问题. FileInputStream 类的主要 ...
- python+sublime text2中文乱码[Decode error - output not utf-8]
转自: http://blog.sina.com.cn/s/blog_765abd7b0101dtbw.html 学习,记录一下.中文编码真的挺麻烦.抽空把自己的sb3的配置写一些. 该问题让我纠结了 ...
- python之单例设计模式
设计模式之单例模式 单例设计模式是怎么来的?在面向对象的程序设计中,当业务并发量非常大时,那么就会出现重复创建相同的对象,每创建一个对象就会开辟一块内存空间,而这些对象其实是一模一样的,那么有没有办法 ...
- 从头编译ARM交叉编译环境
首先Cygwin需安装基本的命令 例如make binutils gcc 还有diffutils 没有他会报找不到cmp命令 这些都可以在setup.exe中找到 编译gcc时,需要注意一个原则:不要 ...
- 关于strcpy的实现.
#include <stdio.h> #include <stdlib.h> int strlen(const char *str) { ; while(*str++!='\0 ...
- JavaScript 反柯里化
浅析 JavaScript 中的 函数 uncurrying 反柯里化 柯里化 柯里化又称部分求值,其含义是给函数分步传递参数,每次传递参数后部分应用参数,并返回一个更具体的函数接受剩下的参数,这中间 ...
- Windows Phone 8.1 发送http 网络请求。
在windows phone 8.1 中可以用 HttpClient 类来发送http 请求. 例子: try { Uri uri = new Uri(@"http://api.map.ba ...
- 动态更换view类的背景----StateListDrawable的应用
StateListDrawable可以根据View的不同状态,更换不同的背景 可以应用如EditText,Button等中,以Button为例 系统中默认的按钮被按下的颜色和未点击时的颜色不一样,该种 ...
- NYOJ541 最强DE 战斗力(第五届省赛试题)
最强DE 战斗力 时间限制:1000 ms | 内存限制:65535 KB 难度: 描述 春秋战国时期,赵国地大物博,资源非常丰富,人民安居乐业.但许多国家对它虎视眈眈,准备联合起来对赵国发起一场 ...