kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo
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.
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的更多相关文章
- 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 ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- 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 ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...
- 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 ...
- 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 ...
随机推荐
- 2011-03-17免Oracle客户端连远程Oracle的方法
1.http://www.oracle.com/technetwork/topics/winsoft-085727.html上下载对应版本的instanctclinet zip包 34M 解压后92M ...
- PHP中交换两个变量的值
首先,采用php的list数据结构.上代码,然后再解析 function swap(&$a, &$b) { list ( $a, $b ) = array ($b, $a ); } l ...
- 【新手向】Centos系统文件权限的系统阐述与演示
在linux服务器日常管理中,我们会经常管理查看文件或者文件夹的权限内容以保证服务的正常运行.今天就和大家聊聊文件权限的那些事. 查看文件的权限情况可以用 ll 命令例: ll -d /kid #查看 ...
- javascript——对象的概念——内建对象
包括内建对象的所有对象都是Object对象的子对象. 1.Array():构建数组的内建构造器函数 例:创建数组方式有两种: 2.Boolean:是对象,与基本数据类型 布尔值 不相同 例:创建Boo ...
- 问题:C#Chart控件自动添加Series;结果:图形组件Chart动态添加Series
Chart1.DataSource = dtb; string[] strcolor = new string[20]; strcolor[0] = "220, 224, 64, 10&qu ...
- LAMP 2.2 Apache配置静态缓存
这里的静态文件指的是图片.js.css 等文件,用户访问一个站点,其实大多数元素都是图片.js.css 等,这些静态文件其实是会被客户端的浏览器缓存到本地电脑上的,目的就是为了下次再请求时不再去服务器 ...
- Shell杀tomcat进程
一.killandclean.sh #!/bin/bash pid=($(ps -ef | grep tomcat | egrep -v grep | awk '{print $2}')) lengt ...
- php中COM函数的使用
php里的com类可以操作window系统上的东西 例如:可以在本地打开一个word文档,然后写入东西,只用于window系统 需要加载php_com_dotnet.dll模块 $word = n ...
- JSON数据格式简介
---------------siwuxie095 JSON 简介 JSON:JavaScript 对象表示法(JavaScript Objec ...
- Ros疑问汇总
一.机器人描述文件三个: 机器人主体body文件: gazebo属性文件: 主文件 smartcar.urdf: 二.启动文件smartcar_display.rviz.launch:启动节点和模拟器 ...