Oulipo (KMP出现次数)
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
KMP板子题
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
void kmp_pre(char x[],int m,int next[]) {
int i,j;
j=next[0]=-1;
i=0;
while(i<m) {
while(-1!=j && x[i]!=x[j])j=next[j];
next[++i]=++j;
}
}
int next[1000010];
int KMP_Count(char x[],int m,char y[],int n) { //x 是模式串,y 是主串
int i,j;
int ans=0;
//preKMP(x,m,next);
kmp_pre(x,m,next);
i=j=0;
while(i<n) {
while(-1!=j && y[i]!=x[j])j=next[j];
i++;
j++;
if(j>=m) {
ans++;
j=next[j];
}
}
return ans;
}
char a[10005];
char b[1000005];
int main() {
int T;
cin>>T;
while(T--) {
scanf("%s",a);
scanf("%s",b);
printf("%d\n",KMP_Count(a,strlen(a),b,strlen(b)));
}
return 0;
}
Oulipo (KMP出现次数)的更多相关文章
- hdu 1686 Oulipo KMP匹配次数统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...
- POJ 3461 Oulipo(KMP,模式串在主串中出现次数 可重叠)
题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...
- [POJ] 3461 Oulipo [KMP算法]
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23667 Accepted: 9492 Descripti ...
- HDU 1686 Oulipo (KMP 可重叠)
题目链接 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispariti ...
- poj3461 Oulipo(KMP模板)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17795 Accepted: 7160 Descripti ...
- Oulipo (kmp)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26857 Accepted: 10709 Descript ...
- POJ 3461 Oulipo KMP
题意:统计其中一个子串的出现次数 题解:即KMP算法中j==m的次数 //作者:1085422276 #include <cstdio> #include <cmath> #i ...
- POJ3461–Oulipo(KMP)
题目大意 给定一个文本串和模式串,求模式串在文本串中出现的次数 题解 正宗KMP 代码: #include<iostream> #include<cstring> #inclu ...
- hdu Oulipo(kmp)
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
随机推荐
- Javascript 面向对象(一):封装
Javascript 面向对象编程(一):封装 Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言, ...
- python爬虫(10)--PyQuery的用法
简介 pyquery 可让你用 jQuery 的语法来对 xml 进行操作.这I和 jQuery 十分类似.如果利用 lxml,pyquery 对 xml 和 html 的处理将更快. 初始化 在这里 ...
- Windows系统上release版本程序bug跟踪解决方案-.dmp文件。
使用场景: Win32程序在release模式下编译完成,发送给最终用户使用时,我们的程序有时候也会出现崩溃的情况,这个时候如果能快速定位崩溃原因或提供一些程序崩溃时的状态信息,对我们解决问题将会带来 ...
- springmvc urlpattern配置详解
静态资源无法访问问题的解决方案: 1.使用Tomcat默认的Servlet解决:在web.xml中加以下代码
- nodejs的POST请求
http://blog.csdn.net/puncha/article/details/9015317 Nodejs 发送HTTP POST请求实例 2013-06-03 17:55 71745人阅读 ...
- C++指针作为函数的参数进行传递时注意的问题
应注意问题: 当指针作为函数的参数进行传递的时候,本质上还是进行的"值传递",也就是复制了一个新的指向该地址的指针变量. 只有在被调函数中,对指针进行引用操作,才可以达到不需要返回 ...
- Luogu 3008 [USACO11JAN]道路和飞机Roads and Planes
BZOJ2200 听说加上slf优化的spfa的卡过,真的不想写这些东西. 考虑使用堆优化的dij算法. 先加上所有双向边,然后dfs一下搜出所有由双向边构成的联通块,然后加上所有的单向边,一边对所有 ...
- C++二进制文件读写
简单二进制文件读写,多文件 /*Demo9.1.cpp*/ #include <iostream> #include <fstream> #include <string ...
- 十三周作业—使用Metaspoit攻击MS08-067
操作过程及结果如下:
- web 打印分页技巧
page-break-after 和 page-break-before: page-break-before和page-break-after CSS属性并不会修改网页在屏幕上的显示,这两个属性是 ...