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模板)的更多相关文章

  1. poj3461 Oulipo (KMP模板题~) 前面哪些也是模板题 O.O

    # include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...

  2. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  3. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

  4. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  5. (模板)poj3461(kmp模板题)

    题目链接:https://vjudge.net/problem/POJ-3461 题意:给出主串和模式串,求出模式串在主串中出现的次数. 思路:kmp板子题. AC代码: #include<cs ...

  6. POJ3461 Oulipo KMP算法

    这个算法去年的这个时候就已经听过了,看毛片算法哈哈..不过理解它确实花了我很久的时间..以致于我一直很排斥字符串的学习,因为总觉得太难了,但是有些硬骨头还是要啃的,这个寒假就啃啃字符串还有一些别的东西 ...

  7. POJ3461–Oulipo(KMP)

    题目大意 给定一个文本串和模式串,求模式串在文本串中出现的次数 题解 正宗KMP 代码: #include<iostream> #include<cstring> #inclu ...

  8. poj3461 Oulipo —— KMP

    题目链接:http://poj.org/problem?id=3461 代码如下: #include<cstdio>//poj 3461 kmp #include<cstring&g ...

  9. Oulipo HDU 1686 KMP模板

    题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...

随机推荐

  1. 【巩固】JS中的封闭空间

    封闭空间的主要思想在于: JS中给一个变量外面加小括号,是不改变任何结果的.比如 var show = function(){ //定义一个名字为show的函数 ); }; show(); //调用名 ...

  2. PBR实现2.0

    之前的错误和欠缺 1. 过于简单的划分diffuse和specular,非常光滑的非金属材料也是很能反光的2. 费奈尔效应的处理,F0的选取也比较随意3. 没有GI,更不支持AO 正确划分diffus ...

  3. Spark数据传输及ShuffleClient(源码阅读五)

    我们都知道Spark的每个task运行在不同的服务器节点上,map输出的结果直接存储到map任务所在服务器的存储体系中,reduce任务有可能不在同一台机器上运行,所以需要远程将多个map任务的中间结 ...

  4. CentOS 7 用户怎样安装 LNMP(Nginx+PHP+MySQL)

    关于 Nginx (发音 “engine x”)这是一款免费.开源.高效的 HTTP 服务器,Nginx是以稳定著称,丰富的功能,结构简单,低资源消耗.本教程演示如何在CentOS 6.5服务器(适用 ...

  5. 实例存储支持的AMI创建步骤

    实例存储支持的AMI创建步骤 一.Windows AMI 1. 选择实例存储支持的AMI创建实例. 2. 远程登录实例进行定制化配置. 3. 通过Web控制台或命令行Bundle实例(并自动上传到S3 ...

  6. IIS7.0上传文件限制的解决方法

    在 Windows7(iis7.5).Win2008(iis 7.0)和Win2003(iis 6.0) 中,默认设置是特别严格和安全的,这样可以最大限度地减少因以前太宽松的超时和限制而造成的攻击. ...

  7. test spring in category

    test tile package com.journaldev.spring.controller; import java.text.DateFormat; import java.util.Da ...

  8. 如何保护.net中的dll文件(防破解、反编译)

    如何保护.net中的dll文件(防破解.反编译) 2010-07-19 15:08 [小 大] 来源: 赛迪网 评论: 0 分享至:      百度权重查询 词库网 网站监控 服务器监控 SEO监控  ...

  9. shell中判读文件存在的方法

    单个文件较简单 if [[ -f filename ]]; then echo exist fi 文件存在并且有内容 if [[ -s filename ]]; then echo exist fi ...

  10. winserver2008 R2 64位 企业版 , IIS 配置运行 asp+access 网站

    新建网站,程序池由DefaultAppPool 改为 Classic .NET AppPool, 并在 高级设置中,把启用 32位应用程序  设为 true 对 access 所在目录新加 every ...