POJ3461 Qulipo(kmp)

传送门

Oulipo

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24124   Accepted: 9679

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

BAPC 2006 Qualification

裸字符串匹配,KMP算法

 1 #include<set>
 2 #include<queue>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 using namespace std;
 9 const int N = 1000010;
10 #define For(i,n) for(int i=1;i<=n;i++)
11 #define Rep(i,l,r) for(int i=l;i<=r;i++)
12 char s1[N],s2[N];
13 int n,T,m,next[N];
14
15 void BuildNext(){
16     int pre=0;n=strlen(s1+1),m=strlen(s2+1);
17     Rep(i,2,n){
18         while(pre&&s1[pre+1]!=s1[i]) pre=next[pre];
19         if(s1[pre+1]==s1[i]) pre++;
20         next[i]=pre;
21     }
22 }
23
24 void KMP(){
25     int is1=0,ans=0;
26     For(is2,m){
27         while(is1&&s1[is1+1]!=s2[is2]) is1=next[is1];
28         if(s1[is1+1]==s2[is2]) is1++;
29         if(is1==n){
30             ans++;
31             is1=next[is1];
32         }
33     }
34     printf("%d\n",ans);
35 }
36
37 int main(){
38     scanf("%d",&T);
39     For(i,T){
40         scanf("%s%s",s1+1,s2+1);
41         BuildNext();
42         KMP();
43     }
44     return 0;
45 }
时间: 2024-11-05 04:35:27

POJ3461 Qulipo(kmp)的相关文章

POJ3461 Oulipo[KMP]【学习笔记】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36916   Accepted: 14904 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 quot

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

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; char a1[1000010],a2[1000010]; int next[1000010]; int len1,len2,cot; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=len1) { if(j==-1||a1[i]==a1[j]

poj3461 Oulipo (kmp)

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35732   Accepted: 14439 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 quot

POJ3461 Oulipo KMP算法应用

题目描述 给定主串和模式串,问模式串在主串中出现的次数 Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN Sample Output 1 3 0 解题思路:KMP算法是找到一个匹配就跳出,这题是要计数,所以我们把KMP算法稍微改一下即可,在找到一个匹配(即j=模式串长度)时计数器++,再从next[j]开始找就好了.详见代码 #include <cstdio> #include <cstring> void Get

[KMP]【学习笔记】

POJ3461 Oulipo KMP裸题 出现几次 关于KMP 字符串从0开始,所以p[i]就是地i+1个字符 f[i]是失配函数,表示已经匹配了i个字符,i+1(就是p[i])失配转移到哪里 令j=f[i],就是说以位置i-1结尾的后缀包括了0...j-1这个前缀,再检查p[j]==s[i](即j+1  i+1) // // main.cpp // poj3461 // // Created by Candy on 10/19/16. // Copyright © 2016 Candy. Al

算法之字符串专题

一.单串匹配问题 poj2406(求字符串的周期)利用next[ ]性质,ans=next[len]%(len-next[len])==0?next[len]/(len-next[len]):1; poj2752(求所有相同的前后缀)利用next[ ]性质,pos=next[pos](不断向前找) next[0]=-1: next[i]=max(相同前后缀) poj3461(kmp模板题) 二.多串匹配问题 poj3080.poj3450(求多串最长公共字串且字典序最小)二分,枚举首串(按字典序

POJ3461 Oulipo 【KMP】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22295   Accepted: 8905 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

POJ-3461 Oulipo(KMP)

题目链接:http://poj.org/problem?id=3461 题目大意: 给你两个字符串p和s,求出p在s中出现的次数. 思路:p在s中KMP匹配,匹配成功,再从next[last]的位置匹配即可,因为允许出现的两次有重叠的部分. //1208 KB 94 ms #include<cstdio> #include<iostream> #include<cstring> using namespace std; int n; char s[1000100],p[

poj3461 字符串匹配 熟悉kmp算法第一题

题意:  计算一个字符串在另一个字符串中出现的次数. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxl=1000000+10; 6 const int maxw=10000+10; 7 char t[maxl],p[maxw]; 8 int T,ans,f[maxw]; 9 void getfail(char *p) 1