KMP算法

求串内匹配数,计数时返回next[]位置。

Problem Description

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.

Input

The 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.

Output

For 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

Source

华东区大学生程序设计邀请赛_热身赛
 
 #include<stdio.h>
#include<memory.h>
#include<string.h>
char a[],b[];
int Next[];
void getn(int s){
int i=,j=-;
memset(Next,,sizeof(Next));
Next[]=-;
while(i<s){
if(j==-||a[i]==a[j]){
i++;
j++;
Next[i]=j;
}
else j=Next[j];
}
}
int getnum(int x,int y){
int i=,j=,k=;
while(i<y&&j<x){ //j<x避免死循环
if(j==-||b[i]==a[j]){ i++; j++; }
else j=Next[j];
if(j==x){
k++;
j=Next[j]; //返回对应next[]位置
}
}
return k;
}
int main()
{
int n,x,y;
scanf("%d",&n);
while(n--){
scanf("%s%s",a,b);
x=strlen(a);
y=strlen(b);
getn(x);
printf("%d\n",getnum(x,y));
}
return ;
}

【KMP】Oulipo的更多相关文章

  1. 【POJ3461】【KMP】Oulipo

    Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...

  2. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  3. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  4. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  5. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

  6. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  7. 【KMP】Radio Transmission

    问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...

  8. 【kmp】似乎在梦中见过的样子

    参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...

  9. 【POJ2752】【KMP】Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

随机推荐

  1. 关于DOS下启动MySQL时提示服务名无效

    主要原因:启动时:net start mysql 而打开服务后发现,本地服务中mysql这个服务实际名字为mysql55,故启动语句应为:net  start mysql55: 以下摘自课程提问: 你 ...

  2. yii 删除内容时增加ajax提示

    环境 : 后台有新闻分类和新闻的文章,在分类下有文章存在的时候,不想用户删除分类 代码 controller public function actionDelete($id) { $data = C ...

  3. Linux服务的管理

    1.Linux服务的介绍 系统服务 --某些服务的服务的对象是Linux系统本身,或者Linux系统系统用户,这类服务我们称为系统服务(System Service) 网络服务 --提供给网络中的其他 ...

  4. pen: Local Testing

    [踩点] * OLEViewer:查看 ActiveX 组件信息 [Fuzz] * Tools in This Article * COMRaider:ActiveX/ocx [utils] * Fi ...

  5. 腾讯云升级到PHP7

    1.删除之前安装的PHP yum remove php* php-common 2.安装yum源 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel ...

  6. HTML学习(1)

    1.缩写和首字母缩写<abbr><acronym> <abbr title="etcetera">etc.</abbr> <a ...

  7. maven第四章背景案例

    4.3简要设计 4.3.1接口设计 4.3.2模块结构 思想 先定义出核心接口,一个接口可以认为是一个功能,根据接口划分功能 设计模式就是一种思想,外观模式和代理模式,适配者模式三者的区别 http: ...

  8. <customErrors>节点说明1

    <customErrors>节点用于定义一些自定义错误信息的信息.此节点有Mode和defaultRedirect两个属性, 其中defaultRedirect属性是一个可选属性,表示应用 ...

  9. php锁表

    用PHP实现mysql锁表 mysql锁表,是利用相关的SQL语句 //执行SQL语句 锁掉userinfo表 $sql = "LOCK TABLES userinfo WRITE" ...

  10. 解释DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

    解释DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci 在创建数据库的时候,经常用到一句:CREATE DATABASE `tpcms` DEFAUL ...