poj3461 Oulipo(KMP模板)
Oulipo
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 17795 | Accepted: 7160 |
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 <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int next[10005];
void getnext(char str[]) {
int j = 0;
int k = -1;
next[0] = -1;
int len = strlen(str);
while (j < len) {
if (k == -1 || str[j] == str[k]) {
j++;
k++;
if (str[j] == str[k])
next[j] = next[k];
else
next[j] = k;
} else {
k = next[k];
}
}
}
int kmp(char p[], char s[]) {
getnext(p);
int j = 0;
int k = 0;
int ans = 0;
int plen = strlen(p);
int slen = strlen(s);
while (j < slen) {
if (k == -1 || s[j] == p[k]) {
j++;
k++;
} else {
k = next[k];
}
if (k == plen) {
ans++;
k = next[k];
}
}
return ans;
}
int main() {
// freopen("in.txt","r",stdin);
int T;
char s[1000005];
char p[10005];//模式串
scanf("%d",&T);
while (T--) {
scanf("%s",p);
scanf("%s",s);
printf("%d\n",kmp(p,s));
}
return 0;
}
poj3461 Oulipo(KMP模板)的更多相关文章
- poj3461 Oulipo (KMP模板题~) 前面哪些也是模板题 O.O
# include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...
- POJ Oulipo KMP 模板题
http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4 ...
- POJ Oulipo(KMP模板题)
题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...
- HDU 1686 - Oulipo - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...
- (模板)poj3461(kmp模板题)
题目链接:https://vjudge.net/problem/POJ-3461 题意:给出主串和模式串,求出模式串在主串中出现的次数. 思路:kmp板子题. AC代码: #include<cs ...
- POJ3461 Oulipo KMP算法
这个算法去年的这个时候就已经听过了,看毛片算法哈哈..不过理解它确实花了我很久的时间..以致于我一直很排斥字符串的学习,因为总觉得太难了,但是有些硬骨头还是要啃的,这个寒假就啃啃字符串还有一些别的东西 ...
- POJ3461–Oulipo(KMP)
题目大意 给定一个文本串和模式串,求模式串在文本串中出现的次数 题解 正宗KMP 代码: #include<iostream> #include<cstring> #inclu ...
- poj3461 Oulipo —— KMP
题目链接:http://poj.org/problem?id=3461 代码如下: #include<cstdio>//poj 3461 kmp #include<cstring&g ...
- Oulipo HDU 1686 KMP模板
题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...
随机推荐
- 手机平板等移动端适配跳转URL的js代码
<script type="text/javascript"> if(/AppleWebKit.*mobile/i.test(navigator.userAgent) ...
- The server encountered an internal error that prevented it from fulfilling this request.
type Exception report message Request processing failed; nested exception is org.mybatis.spring.MyBa ...
- Linux命令(22)find的使用
在linux下面工作,有些命令能够大大提高效率. 比如说find命令,他哥俩可以算是必会的linux命令,几乎每天都要用到他们. find命令 find命令的一般形式 find命令的常用选项及实例 f ...
- Kafka深度解析,众人推荐,精彩好文!
作者: Jason Guo 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度为O(1)的方式提供消息持久化能力,并保证即使对TB级以上数据 ...
- MyEclipse运行前自动保存
MyEclipse中又一坑人设置.运行编译好的代码,如果不手动保存的话,MyEclipse是不会自动保存的.所以,在运行前如果对代码做了修改,运行的依然是上一次的结果. 修改这个反人类设置步骤如下: ...
- IIS发布文件出现:未能加载文件或程序集“xxxx”或它的某一个依赖项。试图加载格式不正确的程序。
解决方案:IIS——应用程序池—选中网站—高级设置——启用32位应用程序 :true.
- 4、IMS
链:1:http://www.cnblogs.com/gnuhpc/archive/2012/12/11/2813494.html [笔记] 1.<计算机网络(第五版)>P10-15:电路 ...
- Node ExpressJs server的路径设置
一.动态页面的路径: app.METHOD(PATH, HANDLER)Where: * app is an instance of express.* METHOD is an HTTP reque ...
- gulp.js基础入门
安装 Node 去 nodejs.org 根据系统选择性按照教程安装Node. 创建项目 创建项目文件夹 进入项目文件夹 初始化项目 使用npm命令:npm init,根据提示完成. 安装 Gulp ...
- Nginx配置location总结及rewrite规则写法
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 32.0px "Helvetica Neue"; color: #323333 } p. ...