一道字符串匹配的题目,仅仅借此题练习一下KMP

因为这道题目就是要求用从头开始的n个字符串去匹配原来的字符串,很明显与KMP中求next的过程很相似,所以只要把能够从头开始匹配一定个数的字符串的个数加起来就OK了(再此结果上还应该加上字符串的长度,因为每个从头开始的字符串本身也可以去匹配自己的),即将next中值不为-1和0的个数统计出来即可。

用GCC编译的,时间用了46MS。

 #include <stdio.h>
#include <string.h>
#define MAXLEN 200005
#define MOD 10007 int next[MAXLEN];
char myChar[MAXLEN]; int getNext()
{
int i = ,j = -;
int sum = ;
int len = strlen(myChar);
memset(next,,sizeof(int)); next[i] = j; while(i < len)
{
if(j == - || myChar[i] == myChar[j])
{
i ++;
j ++;
next[i] = j;
if(j != - && j != )
{
sum ++;
sum = sum % MOD;
}
}
else
{
j = next[j];
}
} return sum;
} int main()
{
int n,m; scanf("%d",&n);
while(n --)
{
scanf("%d",&m);
memset(myChar,,sizeof(char));
scanf("%s",myChar);
printf("%d\n",(getNext()+m)%MOD);
} return ;
}

代码如下:

hdu 3336【Count the string】(KMP)的更多相关文章

  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. 【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 ...

  3. 【HDU 3336 Count the string】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

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

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

  5. HDU 3336 - Count the string(KMP+递推)

    题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...

  6. 【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法

    [KMP] 学习KMP,我们先要知道KMP是干什么的. KMP?KMPLAYER?看**? 正如AC自动机,KMP为什么要叫KMP是因为它是由三个人共同研究得到的- .- 啊跑题了. KMP就是给出一 ...

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

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

  8. HDU3336 Count the string 题解 KMP算法

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

  9. hdu3336 Count the string 扩展KMP

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

随机推荐

  1. GR&R

    ANOVA gauge R&R (or ANOVA gauge repeatability and reproducibility) is a measurement systems anal ...

  2. Accounting_权责发生制和收付实现值的区别(概念)

    2014-07-11 BaoXinjian

  3. AP_AP系列 - 相关设定的简述(概念)

    2014-07-08 Created By BaoXinjian

  4. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  5. JAVA 数组排序

    一.数组升序排序 实例: import java.util.Arrays; //导入数组处理 public class Test{ public static void main(String[] a ...

  6. Python进阶07 函数对象

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 秉承着一切皆对象的理念,我们再次回头来看函数(function).函数也是一个对象 ...

  7. Java 线程间通讯(管道流方式)

    一.管道流是JAVA中线程通讯的常用方式之一,基本流程如下: 1)创建管道输出流PipedOutputStream pos和管道输入流PipedInputStream pis 2)将pos和pis匹配 ...

  8. [SQL]replace替换字符串中的字符

    ','**') --下面是结果集 /* ----------- 12345678** */ SELECT replace(CONVERT(varchar(),GETDATE(),),'-','') - ...

  9. [HDU 5135] Little Zu Chongzhi's Triangles (dfs暴搜)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135 题目大意:给你n条边,选出若干条边,组成若干个三角形,使得面积和最大.输出最大的面积和. 先将边 ...

  10. (转)windows下配置nginx+php环境

    原文地址 http://www.cnblogs.com/huayangmeng/archive/2011/06/15/2081337.html 刚看到nginx这个词,我很好奇它的读法(engine ...