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 在主串中匹配到j>=plen时,j继续回溯。
 #include<stdio.h>
#include<string.h>
int Next[],n,m,_,tlen,plen;
char t[],p[]; void prekmp() {
tlen=strlen(t);
plen=strlen(p);
int i,j;
j=Next[]=-;
i=;
while(i<plen) {
while(j!=-&&p[i]!=p[j]) j=Next[j];
if(p[++i]==p[++j]) Next[i]=Next[j]; //这里判断的是模式串
else Next[i]=j;
}
} int kmp() {
prekmp();
int i,j,ans=;
i=j=;
while(i<tlen) {
while(j!=-&&t[i]!=p[j]) j=Next[j];
i++;j++;
if(j>=plen) { //主要区别
ans++;
j=Next[j];
}
}
return ans;
} int main() {
for(scanf("%d",&_);_;_--) {
scanf("%s",p);
scanf("%s",t);
printf("%d\n",kmp());
}
}

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. python django ORM 性能优化 select_related & prefetch_related

    q = models.UserInfo.objects.all() select * from userinfo select * from userinfo inner join usertype ...

  2. Mysql数据库服务器配置文件/etc/my.cnf的详细配置

    以下是 Mysql数 据库服务器配置文件 /etc/my.cnf的详细配置.应用场合是 InnoDB引擎, 4核 CPU, 32位SUSE.   [client] port        = 3306 ...

  3. 问题:C#将base64转换成二进制图片;结果:c# Base64编码和图片的互相转换代码

    c# Base64编码和图片的互相转换代码 Base64编码在Web方面有很多应用,譬如在URL.电子邮件方面.网上有很多相关的资源用于提供Base64编码和其他编码的转换,.Net Framewor ...

  4. opengl版本和扩展

    检查自己的OpenGL版本 可以很容易的知道自己系统中的OpenGL版本,方法就是调用glGetString函数. const char* version = (const char*)glGetSt ...

  5. 我的第一个Socket程序-SuperSocket使用入门(一)

    第一次使用Socket,遇到过坑,也涨过姿势,网上关于SuperSocket的教程基本都停留在官方给的简单demo上,实际使用还是会碰到一些问题,所以准备写两篇博客,分别来介绍SuperSocket以 ...

  6. JAVA基础知识总结8(设计模式)

    设计模式:JAVA中有23种设计模式 1.解决问题最行之有效的思想. 2.是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 3.使用设计模式是为了可重用代码.让代码更容易被他人理解 ...

  7. 第3章 springboot接口返回json 3-1 SpringBoot构造并返回一个json对象

    数据的使用主要还是以JSON为主,我们不会去使用XML. 这个时候我们先不使用@RestController,我们使用之前SpringMVC的那种方式,就是@Controller.  @Respons ...

  8. Condition实现等待、通知

    使用Condition实现等待/通知: import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.L ...

  9. Luogu 4137 Rmq Problem / mex

    一个主席树题. 一开始想着直接动态开点硬搞就可以了,每次查询只要作一个类似于前缀和的东西看看区间有没有满,在主席树上二分就可以了. 但是这样是错的,因为一个权值会出现很多次……然后就错了. 所以我们考 ...

  10. 前端基础 之 BOM和DOM

    浏览目录 背景 BOM window对象 window的子对象 DOM HTML DOM树 查找标签 节点操作 事件 一.背景 到目前为止,我们已经学过了JavaScript的一些简单的语法.但是这些 ...