题意:

求一个字符串的所有前缀串的匹配次数之和.

思路:

首先仔细思考: 前缀串匹配.

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]的更多相关文章

  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 next函数的应用 + DP )

    dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...

  4. HDU 3336 Count the string KMP

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

  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(next数组运用)

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

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

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

  9. hdu 3336 count the string(KMP+dp)

    题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...

随机推荐

  1. npoi 使用方法

    不少朋友在做项目的过程中或多或少的都会用到excel导入.导出功能,下面我来给大家介绍一下使用NPOI如何导入.导出excel, 通过代码描述一下npoi的使用方法,希望都够给需要的朋友一点点帮助,也 ...

  2. java中关于SSL/TSL的介绍和如何实现SSL Socket双向认证

    一.        SSL概述 SSL协议采用数字证书及数字签名进行双端实体认证,用非对称加密算法进行密钥协商,用对称加密算法将数据加密后进行传输以保证数据的保密性,并且通过计算数字摘要来验证数据在传 ...

  3. [C#参考]利用Socket连续发送数据

    这个例子只是一个简单的连续发送数据,接收数据的DEMO.因为最近做一个项目,要求robot连续的通过Socket传回自己的当前的位置坐标,然后客户端接收到坐标信息,在本地绘制地图,实时显示robot的 ...

  4. win10中的vmware桥接模式异常,不能设置同网段ip

    今天发现centOS的桥接模式不正常了,配置 dhclient居然不成功,没有同个网段的ip,于是各种找原因. 突然灵光一现,前几天更新了win10,会不会神马驱动或者服务没安装? 找了一圈,我勒个去 ...

  5. 重拾javascript动态客户端网页脚本

    笔记一: 一.DOM 作用: ·              DOM(Doument Object Model) 1.document文档 HTML 文件 (标记语言) <html> < ...

  6. html 转 js 字符串

    看到一个牛人的博客  http://riny.net/lab/#tools_html2js 看了下他的代码  挺棒的 所依赖的两个库在这里 https://github.com/Bubblings/l ...

  7. 7_Table Views

    7 // // ViewController.swift // Table Views // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. ...

  8. Github Blog 搭建手册

    http://www.ilehao.com/blog/2012/11/11/github-blog-config/ http://www.freebuf.com/articles/web/25613. ...

  9. Pro/E 5.0安装图解教程(也适用于Creo Elements/Pro 5.0)

    安装前必读:☆ 本教程适用于 32 位 proe 5.0 M010,M020,M030,M040,M050,M060 过程完全一样:☆ 本教程用于 64 位 proe 5.0 M010,M020,M0 ...

  10. VC中使用GetModuleFileName获取应用程序路径

      .\\与API函数GetModuleFileName获取应用程序目录有何不一样? 采用.\\也能获得应用程序目录,采用GetModuleFileName也能获得,二者有何不同? 一样!一个是相对路 ...