hdu 3336【Count the string】(KMP)
一道字符串匹配的题目,仅仅借此题练习一下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)的更多相关文章
- 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) ...
- 【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 ...
- 【HDU 3336 Count the string】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- HDU 3336 - Count the string(KMP+递推)
题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...
- 【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法
[KMP] 学习KMP,我们先要知道KMP是干什么的. KMP?KMPLAYER?看**? 正如AC自动机,KMP为什么要叫KMP是因为它是由三个人共同研究得到的- .- 啊跑题了. KMP就是给出一 ...
- POJ 3336 Count the string (KMP+DP,好题)
参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...
- HDU3336 Count the string 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...
- hdu3336 Count the string 扩展KMP
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
随机推荐
- SQLServer分页存储过程
创建存贮过程: Create PROCEDURE [dbo].[UP_GetRecordByPage]@tblName varchar(255), -- 表名@fldName varc ...
- PLSQL_Oracle物化视图Material View的基本概念和用法 (概念)
2014-06-08 Created By BaoXinjian
- CF 500 B. New Year Permutation 并查集
User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permut ...
- angular.js 的angular.copy 、 angular.extend 、 angular.merge
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 中文unicode范围及unicode编解码
中文unicode范围 : [\u4e00-\u9fa5] 普通字符串可以用多种方式编码成Unicode字符串,具体要看你究竟选择了哪种编码:unicodestring = u"Hello ...
- js封装 与 js高级用法 问题集合
1. 什么是自执行的匿名函数? 它是指形如这样的函数: (function {// code})(); 2. 疑问 为什么(function {// code})();可以被执行, 而function ...
- ruby 字符串学习笔记1
1 从一种数据结构中构件字符串 hash = { key1: "val1", key2: "val2" } string = "" hash ...
- [ActionScript 3.0] AS3动画类Tweener中滤镜的运用
package { import caurina.transitions.Tweener; import caurina.transitions.properties.FilterShortcuts; ...
- easyUi中的一段漂亮代码之将list转换成tree.
function convert(rows){ function exists(rows, parentId){ for(var i=0; i<rows.length; i++){ if (ro ...
- Remove Duplicates from Sorted List(链表)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...