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. (PHP)程序中如何判断当前用户终端是手机等移动终端

    推荐: Mobile-Detect:https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.php Detect ...

  2. Linux系统的压缩技术

    1.常见的压缩文件扩展名 *.Z ---> compress程序压缩的文件. *.gz --->gzip 程序压缩的文件: *.bz2------>bzip2程序压缩的文件: *.t ...

  3. eclipse中 报出The type javax.servlet.http.HttpServlet cannot be resolved. It is indirect错误

    在Myeclispe部署项目后 报错 The type javax.servlet.http.HttpServlet cannot be resolved. It is indirect错误 如果在M ...

  4. ios项目上传svn丢失*.a文件

    Win TortoiseSVN:鼠标右键-TortoiseSVN-Settings-General-Subversion-Golobal ignore pattern Win Subversion:编 ...

  5. [DFNews] Elcomsoft 发布EPPB 2.00.233

    Elcomsoft Phone Password Breaker 是俄罗斯Elcomsoft公司推出的手机取证工具,能够针对黑莓.苹果等手机的备份文件进行多种方式破解,支持远程获取iCloud数据. ...

  6. Emergency(山东省第一届ACM省赛)

    Emergency Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Kudo’s real name is not Kudo. H ...

  7. dubbo入门示例

    前提准备: 在本次实验之前,需要准备一下几个包: Spring中的aop.beans.context.core.expression以及struts中的commons-logging.javassis ...

  8. C++设计模式-Composite组合模式

    Composite组合模式作用:将对象组合成树形结构以表示“部分-整体”的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. UML图如下: 在Component中声明所有用来 ...

  9. Linux内核模块设计

    内核的设计有两种方式:单内核和微内核,两者各有优劣,关于两者的比较可以参见wiki.windowds和Solaris采用微内核结构. Linux内核采用单内核结构,设计比较简单,但单内核的理念是把所有 ...

  10. iOS10 权限适配

    权限适配 这应该算iOS10系统适配的范畴,最近这两个都在弄,所以就直接和Xcode8适配一起写出来了. 在iOS10之后需要在Info.plist中,添加新的字段获取权限,否则在iOS10上运行会导 ...