一道字符串匹配的题目,仅仅借此题练习一下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. 杀死future处理的阻塞线程

    public class Test{ public static void main(String[] args){ ExecutorService pool = Executors.newFixed ...

  2. 并发容器之ConcurrentSkipListSet

    概要 本章对Java.util.concurrent包中的ConcurrentSkipListSet类进行详细的介绍.内容包括:ConcurrentSkipListSet介绍ConcurrentSki ...

  3. LNMP环境搭建配置memcache

    原始出处  http://iceeggplant.blog.51cto.com/1446843/819576 memcached是高性能的,分布式的内存对象缓存系统,在动态应用中减少数据库负载,提升访 ...

  4. WCF bindings comparison z

    Binding Protocol/Transport Message Encoding Security Default Session Transaction Duplex BasicHttpBin ...

  5. windows service的作成

    http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html

  6. HTML readyState 属性 iframe onreadystatechange事件

    -----------readyState--------------- 定义和用法 readyState 属性返回当前文档的状态(载入中……). 该属性返回以下值: uninitialized - ...

  7. ios开发下的点透处理

    界面上有一个浮动的div,这个div下面有一个文本框,当给这个div绑定touchstar后,做了冒泡的处理代码,还是会出现点透现象,触发下面的的文本框事件.立马弹出一个输入法出来. 1.网上有一种方 ...

  8. 【转】Python处理HTML转义字符

    Python处理HTML转义字符 转 [http://www.cnblogs.com/xuxn/archive/2011/08/12/parse-html-escape-characters-in-p ...

  9. 如何获取SQL Server 2008数据库数据文件的位置

    在SQL Server中,要获取数据库数据文件的物理位置,有三种方法: sp_helpdb bright name          filename                        f ...

  10. 无法加载程序集,因为该程序集包含EdmSchemaAttribute,并按名称加载结束类型。不允许同时按名称和特性进行加载

    小小记录下,今天写程序时遇到的一个bug,相信大家用MVC+EF写项目时遇到的概率是挺大的.昨天本人也遇到了,很悲剧,暂时没想到好的办法解决,跟项目经理交流了下,这个问题认为出现在EF的自身设计上,它 ...