The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book:

Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.

InputThe first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W). 
One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000. 
OutputFor every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

Sample Input

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

Sample Output

1
3
0
题意:还是找最大匹配,但是这次可以重复
题解:刚开始用的上一题的第一个方法,直接暴力遍历字符串结果tle,发现可以利用字符串kmp本身的特点处理
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; int Next[N],slen,plen,res;
string str,ptr; void getnext()
{
int k=-;
Next[]=-;
for(int i=;i<=slen-;i++)
{
while(k>-&&str[k+]!=str[i])k=Next[k];
if(str[k+]==str[i])k++;
Next[i]=k;
}
}
void kmp()
{
int k=-;
for(int i=;i<plen;i++)
{
while(k>-&&str[k+]!=ptr[i])k=Next[k];
if(str[k+]==ptr[i])k++;
if(k==slen-)
{
res++;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
// cout<<setiosflags(ios::fixed)<<setprecision(2);
int t;
cin>>t;
while(t--){
cin>>str>>ptr;
slen=str.size();
plen=ptr.size();
getnext();
res=;
kmp();
cout<<res<<endl;
}
return ;
}

hdu1686字符串kmp的更多相关文章

  1. hdu 5510 Bazinga(字符串kmp)

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  2. 模板—字符串—KMP(单模式串,单文本串)

    模板—字符串—KMP(单模式串,单文本串) Code: #include <cstdio> #include <cstring> #include <algorithm& ...

  3. 字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组

    涉及到字符串的问题,无外乎这样一些算法和数据结构:自动机 KMP算法 Extend-KMP 后缀树 后缀数组 trie树 trie图及其应用.当然这些都是比较高级的数据结构和算法,而这里面最常用和最熟 ...

  4. 【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)

    题意:求n个串的字典序最小的最长公共子串. 解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串.从大长度开始枚举,找到了就break挺快的.而且KMP的作用就是匹配子串, ...

  5. 数据结构(复习)---------字符串-----KMP算法(转载)

    字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...

  6. 字符串(KMP):BZOJ 3670 [Noi2014]动物园

    3670: [Noi2014]动物园 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1521  Solved: 813[Submit][Status] ...

  7. HDU 4668 Finding string (解析字符串 + KMP)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一个压缩后的串,以及一个模式串,问模式串 ...

  8. 流动python - 字符串KMP匹配

    首先我们看一下简单的字符串匹配. 你可以把文本字符串s固定,模式字符串p从s对齐的左边缘,作为承担部分完全一致,匹配成功,失败将是模式字符串p整体向右1地点,继续检查对齐部分,重复. #朴素匹配 de ...

  9. 查找子字符串----KMP算法深入剖析

    假设主串:a b a b c a b c a c b a b      子串:a b c a c 1.一般匹配算法 逐个字符的比较,匹配过程如下: 第一趟匹配 a b a b c a b c a c ...

随机推荐

  1. Linux服务器配置---ntp

    配置ntp     ntp就是网络时间同步的服务,时间的准确性非常重要,很多数据在记录时都要知道准确的时间.网上有很多站点,一般国内会设置匹配中科院国家授时中心的时间. 1.安装ntp软件 [root ...

  2. Xcode10.x适配的部分问题

    因为我们项目是一个Workspace,由若干个库组成(组件化比较碎),又涉及到海外和国内(存在很多差异性),整个项目的编译是由每个库的脚本(每个库生成会支持32位和64位,每次编译前会清除历史缓存), ...

  3. USB开发库STSW-STM32121文件分析

    hw_config.c: 该文件中包含系统配置的函数. usb_desc.c:各种描述符 usb-endp.c:就两个函数分别处理端点1的IN和端点2的OUT. usb_istr.c: 该文件中只有一 ...

  4. 20180112final和static

    final关键字 fianl关键字可以修饰类.成员变量.和方法中的局部变量. (java中的局部变量?定义在方法中的变量都是局部变量,使用前必须初始化) 1.fianl类 Final类不能被继承,即可 ...

  5. 轻量级文本标记语言-Markdown

    Markdown简介 接触过github的都知道,在发布项目的时候可以建立一个说明文件README.md,这个md文件就是Markdown文本编辑语言的文件. Markdown 是一种轻量级标记语言, ...

  6. Arduino使用HC05蓝牙模块与手机连接

    Arduino使用HC05蓝牙模块与手机连接 一切都是最好的选择 首先是线路连接,一定不要接错了 Arduino 代码 #include <SoftwareSerial.h> // Pin ...

  7. django 加载静态文件(图片,js,css)

    昨天写过一个项目通过django上传展示图片,但是今天写项目的时候发现出现了问题,静态文件加载不出来了,尴尬的一笔~ 记录一下静态文件的使用方法,基础~ ----------------------- ...

  8. Android http通信案例

    Android studio 编写安卓程序,实现 http 通信,获得百度主页源代码. -------------------------------------------------------- ...

  9. 20145322 《网络对抗》 MSF基础应用1

    20145322何志威 Exp5 MS08_067漏洞测试 实验问答 什么是exploit.payload.encode exploit:通过一个漏洞对程序进行攻击的过程 payload:有具体功能作 ...

  10. 超频真的不难!G3258超频4.5GHz全攻略

    奔腾G3258搭配主板详解 [pconline 应用]目前DIY市场上最火热的装机组合莫过于奔腾20周年纪念版处理器G3258搭配B85芯片组主板,只要通过适当的超频,相对较低投入也能来不错的性能体验 ...