题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336

题意就是求串s的前缀的个数和;

例如:abab

前缀 个数

a     2

ab    2

aba   1

abab  1

总数:6

dp[i] 表示前面的字符以s[i-1]结尾的前缀个数;上列中dp[4]=2(以最后一个字符b结尾的前缀) {abab,ab};

可以看出增加一个字母会产生一个新的前缀,那就是整个串,之前的前缀就是Next[i]的位置所对应的dp,即dp[Next[i]];

所以dp[i] = dp[Next[i]] + 1;-_-主要是仔细的想一下,有点绕;

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; const int N = 2e6+;
const int mod = ; char s[N];
int dp[N], n, Next[N]; void GetNext()
{
int i=,j=-;
Next[] = -;
while(i<n)
{
if(j==- || s[i]==s[j])
Next[++i] = ++j;
else
j = Next[j];
}
}
int main()
{
int T, sum;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
scanf("%s", s);
GetNext();
memset(dp, , sizeof(dp));
sum = ;
for(int i=; i<=n; i++)
{
dp[i] = dp[Next[i]] + ;
sum = (sum + dp[i]) % mod;
}
printf("%d\n", sum);
}
return ;
}

Count the string---hdu3336(kmp Next数组的运用)的更多相关文章

  1. 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) ...

  2. Count the string[HDU3336]

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

  3. hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】

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

  4. hdu3336 Count the string 扩展KMP

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  5. HDU3336 Count the string 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...

  6. 【HDU 3336】Count the string(KMP+DP)

    Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...

  7. hdu 3336【Count the string】(KMP)

    一道字符串匹配的题目,仅仅借此题练习一下KMP 因为这道题目就是要求用从头开始的n个字符串去匹配原来的字符串,很明显与KMP中求next的过程很相似,所以只要把能够从头开始匹配一定个数的字符串的个数加 ...

  8. POJ 3336 Count the string (KMP+DP,好题)

    参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...

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

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

  10. hdu_3336: Count the string(KMP dp)

    题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...

随机推荐

  1. windows测试模式

    测试模式通常意义就是让windows 操作系统在测试状态下运行,windows操作系统在这种模式下可以运行非官方或无数字签名的驱动程序 .   目录 1 定义 2 进入/退出windows测试模式方法 ...

  2. webservice文件上传下载

    使用DataHandler实现webservice的文件上传下载 服务端代码: package com.hello.weChat.controller; import javax.activation ...

  3. 1.重学javascript (一)

    一.script标签解析 <script>xxx</script>这组标签,是用于在html 页面中插入js的主要方法.它主要有以下 几个属性: 1.charset:可选.表示 ...

  4. sqlServer的主键只能自增不能手动增加

    1. 2.找到相应的表,找到表设计.

  5. TensorFlow基础笔记(7) 图像风格化效果与性能优化进展

    参考 http://hacker.duanshishi.com/?p=1693http://blog.csdn.net/hungryof/article/details/53981959http:// ...

  6. 应用于Java中的一个开源的表达式语言(Expression Language)

    OGNL(英文全称:Object Graph Navigation Language,中文名:对象导航图语言)是应用于Java中的一个开源的表达式语言(Expression Language),它被集 ...

  7. 下列JSP代码:

    下列JSP代码: <html> <body> <% for(int i = 0; i < 10; i++) { //1 } %> </body> ...

  8. Restful Web Service初识

    一.Web Services Web Services 是一种基于组件的软件平台,是面向服务的Internet 应用.Web Services 框架的核心技术包括SOAP ,WSDL 和UDDI ,它 ...

  9. vector使用注意事项

    1. 需要保存若干同类数据项时,用vector比较方便.不用管理下标,直接push_back即可. 2. 当程序重复执行一段代码时,之前保存数据用的vector需要清空.因为不是按照下标保存的数据,不 ...

  10. EasyDarwin开发出相似于美拍、秒拍的短视频拍摄SDK:EasyVideoRecorder

    EasyVideoRecorder Github:https://github.com/EasyDarwin/EasyVideoRecorder EasyVideoRecorder作为一款短视频拍摄的 ...