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

  1. 3
  2. BAPC
  3. BAPC
  4. AZA
  5. AZAZAZA
  6. VERDI
  7. AVERDXIVYERDIAN

Sample Output

  1. 1
  2. 3
  3. 0
    题意:还是找最大匹配,但是这次可以重复
    题解:刚开始用的上一题的第一个方法,直接暴力遍历字符串结果tle,发现可以利用字符串kmp本身的特点处理
  1. #include<map>
  2. #include<set>
  3. #include<cmath>
  4. #include<queue>
  5. #include<stack>
  6. #include<vector>
  7. #include<cstdio>
  8. #include<iomanip>
  9. #include<cstdlib>
  10. #include<cstring>
  11. #include<iostream>
  12. #include<algorithm>
  13. #define pi acos(-1)
  14. #define ll long long
  15. #define mod 1000000007
  16. #define ls l,m,rt<<1
  17. #define rs m+1,r,rt<<1|1
  18.  
  19. using namespace std;
  20.  
  21. const double g=10.0,eps=1e-;
  22. const int N=+,maxn=+,inf=0x3f3f3f3f;
  23.  
  24. int Next[N],slen,plen,res;
  25. string str,ptr;
  26.  
  27. void getnext()
  28. {
  29. int k=-;
  30. Next[]=-;
  31. for(int i=;i<=slen-;i++)
  32. {
  33. while(k>-&&str[k+]!=str[i])k=Next[k];
  34. if(str[k+]==str[i])k++;
  35. Next[i]=k;
  36. }
  37. }
  38. void kmp()
  39. {
  40. int k=-;
  41. for(int i=;i<plen;i++)
  42. {
  43. while(k>-&&str[k+]!=ptr[i])k=Next[k];
  44. if(str[k+]==ptr[i])k++;
  45. if(k==slen-)
  46. {
  47. res++;
  48. }
  49. }
  50. }
  51. int main()
  52. {
  53. ios::sync_with_stdio(false);
  54. cin.tie();
  55. // cout<<setiosflags(ios::fixed)<<setprecision(2);
  56. int t;
  57. cin>>t;
  58. while(t--){
  59. cin>>str>>ptr;
  60. slen=str.size();
  61. plen=ptr.size();
  62. getnext();
  63. res=;
  64. kmp();
  65. cout<<res<<endl;
  66. }
  67. return ;
  68. }

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. 最新版Intellij IDEA插件JRebel 7.0.7官方免费激活

    本文转自:http://blog.csdn.net/u012283609/article/details/70213307 开场语 有时候真实比小说更加荒诞,因为虚构是在一定逻辑下进行的,而现实往往毫 ...

  2. Nginx能做什么

    本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可能介绍的不完整,毕竟只是我个人使用过和了解到过得.所以还请见谅,同时欢迎留言交流. Ngi ...

  3. 在IBM学到的东西,到底对我的程序生涯产生了多大的影响

    我和很多人交流过一个有趣的现象,那就是刚毕业到30岁这段时间,会觉得时间过得很慢,总觉得自己还很年轻,但是一旦过了30岁,时间就如白驹过隙,一年又一年飞逝而过. 我自己也是,眼瞅着毕业快15年了,15 ...

  4. nginx作防盗链设置

    盗链是一种损害原有网站合法权益,给原网站所在服务器造成额外负担的非法行为. 盗链的实现原理: 客户端向服务器请求资源时,为了减少网络带宽,提高响应时间,服务器一般不会一次将所有资源完整地传回给客户端. ...

  5. servlet+jsp+java实现Web应用

    servlet+jsp+java实现Web应用 环境: 1,eclipse 2,tomcat3,eclipse tomcat 插件 开发过程: 1,建立一个Dynamic Web Project 2, ...

  6. oracle、Mysql数据库客户端DbVisualizer安装

    原文链接:https://jingyan.baidu.com/article/454316ab675302f7a7c03a9e.html

  7. 20145302张薇 《网络对抗》MSF应用基础

    20145302张薇 <网络对抗>MSF应用基础 实验内容 掌握metasploit的基本应用方式 1.主动攻击--ms08_067 2.针对浏览器的攻击--ms11_050 3.针对客户 ...

  8. cron表达式增加一段时间变为新的表达式

    cron表达式是使用任务调度经常使用的表达式了.对于通常的简单任务,我们只需要一条cron表达式就能满足.但是有的时候任务也可以很复杂. 最近我遇到了一个问题,一条任务在开始的时候要触发A方法,在结束 ...

  9. cmder的使用和编码问题解决

    cmder 是一款 windows 下的命令集合软件,它可以集合各种系统下的命令,并且操作非常快速方便.安装有两个版本,一个是简化版(4.27M),一个是完全版(75.7M),它们的唯一区别:完全版包 ...

  10. 'curl' is not recognized as an internal or external command

    使用everything搜索本地的curl.exe发现如下 官网查看最新版本https://curl.haxx.se/windows/ 2019-03-06 最新版本7.64.0 curl-7.64. ...