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
  4.  
  5. 在主串中匹配到j>=plen时,j继续回溯。
  6.  
  1. #include<stdio.h>
  2. #include<string.h>
  3. int Next[],n,m,_,tlen,plen;
  4. char t[],p[];
  5.  
  6. void prekmp() {
  7. tlen=strlen(t);
  8. plen=strlen(p);
  9. int i,j;
  10. j=Next[]=-;
  11. i=;
  12. while(i<plen) {
  13. while(j!=-&&p[i]!=p[j]) j=Next[j];
  14. if(p[++i]==p[++j]) Next[i]=Next[j]; //这里判断的是模式串
  15. else Next[i]=j;
  16. }
  17. }
  18.  
  19. int kmp() {
  20. prekmp();
  21. int i,j,ans=;
  22. i=j=;
  23. while(i<tlen) {
  24. while(j!=-&&t[i]!=p[j]) j=Next[j];
  25. i++;j++;
  26. if(j>=plen) { //主要区别
  27. ans++;
  28. j=Next[j];
  29. }
  30. }
  31. return ans;
  32. }
  33.  
  34. int main() {
  35. for(scanf("%d",&_);_;_--) {
  36. scanf("%s",p);
  37. scanf("%s",t);
  38. printf("%d\n",kmp());
  39. }
  40. }
  1.  

kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo的更多相关文章

  1. kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  2. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  4. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

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

  5. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条

    一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...

  8. kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence

    Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)

    After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...

随机推荐

  1. JavaScript基本概念C - 真与假

    真与假 与 c 和 c++ 非常相似, 但与 Java 不同, JS中被认为true或false范围很广.所有对象 (空字符串除外) 和非零数字都被视为 true.空字符串.零.null 和undef ...

  2. Mybatis 内置 Java 类型别名与 typeHandlers

    aliases There are many built-in type aliases for common Java types. They are all case insensitive, n ...

  3. oracle connect by用法篇 (包括树遍历)之一

    1.基本语法 select * from table [start with condition1] connect by [prior] id=parentid 一般用来查找存在父子关系的数据,也就 ...

  4. Android编译系统产品线

    1.Android源码中的产品线解析 通常产品厂商在拿到Android源码后会在Android源码基础上进行定制修改,以匹配适应自己的产品.这就引入了产品线的概念.Android系统源码中,产品相关的 ...

  5. oracle时间段查询-从00:00:00开始

    之所以记录一下这篇博文,是因为前段时间搞的一个查询发现要从00:00:00这个时间段开始,必须要通过拼接字符串. <select id="queryApplyProgressList& ...

  6. python之特殊方法

    特殊方法的定义: 1.定义在某些class当中 2.不需要直接调用 3.Python的某些函数或者是操作符会调用相应的特殊方法 特殊方法很多,我们只需要编写用到的特殊方法,以及有关联性的特殊方法. — ...

  7. dp-矩阵连乘

    参考:http://blog.csdn.net/liufeng_king/article/details/8497607 使用备忘录算法复杂度降至O(n^3) #include<stdio.h& ...

  8. c++primer-p101.ex3.24

    要求使用迭代器 读入一组整数并把它们存入一个vector对象 1. 将相邻每对整数和输出 2. 先输出第一个和最后一个数的和,然后是第二个和倒数第二个...等等 自己写的: #include<i ...

  9. TCP/IP 笔记 1.1 概 述

    四个层次 每一层负责不同的功能:1) 链路层,有时也称作数据链路层或网络接口层,通常包括操作系统中的设备驱动程序和计算机中对应的网络接口卡.它们一起处理与电缆(或其他任何传输媒介)的物理接口细节.2) ...

  10. 每天一道算法题(14)——N个降序数组,找到最大的K个数

     题目: 假定有20个有序数组,每个数组有500个数字,降序排列,数字类型32位uint数值,现在需要取出这10000个数字中最大的500个. 思路 (1).建立大顶堆,维度为数组的个数,这里为20( ...