题目链接: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. oracle中select clob的返回类型

    当select的字段是clob类型的数据时,但是数据长度在2000字节到4000字节时,默认转为long类型. 所以当用insert into select的时候,预期插入的是clob类型,但是报or ...

  2. am335x gpio 控制的另一种方法

    #include <linux/gpio.h>  #include <linux/module.h>  #include <linux/kernel.h>  #in ...

  3. 决Ubuntu使用`make menuconfig`配置Linux 内核时,出现缺少'ncurses-devel'库支持。

    *** Unable to find the ncurses libraries or the *** required header files. *** 'make menuconfig' req ...

  4. Android——excise(用线性布局、表格布局、相对布局做发送邮件界面)

    LinearLayout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...

  5. 百度JS模板引擎

    1. 应用场景 前端使用的模板系统  或  后端Javascript环境发布页面 2. 功能描述 提供一套模板语法,用户可以写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片段,渲染不同 ...

  6. maven编译插件版本配置案例

    <!-- Build Settings 构建设置 --> <build> <finalName>${project.artifactId}</finalNam ...

  7. 解决Maven项目 Missing artifact jdk.tools:jdk.tools:1.7的错误

    因学习项目需要,在pom.xml添加hbase-client依赖的时候提示解决Maven工程中报 Missing artifact jdk.tools:jdk.tools:1.7的提示信息,之前遇到这 ...

  8. oracle起定时任务,每隔1秒执行一次

    创建一个测试表和一个存储过程: create table a(a date); create or replace procedure test as begin insert into a valu ...

  9. Java.io下的方法是对磁盘上的文件进行磁盘操作

    File类(java.io.*)可表示一个文件,也有可能是一个目录(在JAVA中文件和目录都属于这个类中,而且区分不是非常的明显). Java.io下的方法是对磁盘上的文件进行磁盘操作,但是无法读取文 ...

  10. ssh免密码登录的几个注意事项

    1, authorized_keys文件中每个公钥占一行,不能分成多行. 2,文件夹默认权限为600 3,如果遇到奇怪的问题,可以把.ssh/文件全部删掉,重新用ssh-keygen生成.