题目链接: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. w3c html dom

    http://www.w3school.com.cn/ SQL语句学习 http://www.w3school.com.cn/sql/sql_like.asp

  2. oozie开发注意事项

    ooziejob执行后 1. job.properties.coordinatior.xml中设置的值都是不可变的,除非将job kill掉,然后重新调度. oozie job -kill 00000 ...

  3. 1.javascript语言精粹笔记

    一.注释 /**/ // 采用这个 二.标识符 标识符被用于语句.变量.参数.属性名.运算符和标记三.数字 javascript只有一个单一的数字模型.它在内部被表示64位的浮点数. 没有分离出整形, ...

  4. EasyUI 创建对话框

    对话框是特殊的窗口,它能包括上面的工具栏和下面的按钮.默认对话框不能改变大小,但是用户可以设置resizeable属性为true来使它可以被改变大小:对话框非常简单,可以使用DIV标记创建: < ...

  5. noip2014滚粗记

    滚粗了..伤心. day0:和baba一起去,但是整天都是下雨啊好不爽,鞋子都湿了啊好不爽,注定是要滚粗?在火车站等了1h后上动车走人...在此期间我还天真的认为火车站的wifi可以被我给破解然后上网 ...

  6. 【BZOJ】1629: [Usaco2007 Demo]Cow Acrobats(贪心+排序)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1629 这题我想了很久都没想出来啊... 其实任意两头相邻的牛交换顺序对其它牛是没有影响的.. 那么我 ...

  7. xsocket:空闲超时问题。

    XSocket是什么? java的nio的封装. 详情: 1. http://xsocket.sourceforge.net/core/apidocs/2_1/index.html 2. http:/ ...

  8. VC++分页打印实现

    VC++分页打印实现:  C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243 ...

  9. System.in中的read()方法

    大家先来看例如以下这个程序 public class TestInputStream { public static void main(String args[]) throws IOExcepti ...

  10. iOS开发之-- 设置启动图片

    一.添加启动图片 点击Assets.xcassets进入图片管理,右击,弹出"New Launch Image"或点下面的+号创建Launch Image: 如图,右侧的勾选可以让 ...