【POJ3461】【KMP】Oulipo

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

【分析】

模板

 1 /*
 2 宋代李之仪
 3 卜算子·我住长江头
 4 我住长江头,君住长江尾。日日思君不见君,共饮长江水。
 5 此水几时休,此恨何时已。只愿君心似我心,定不负相思意。
 6 */
 7 #include <iostream>
 8 #include <cstdio>
 9 #include <algorithm>
10 #include <cstring>
11 #include <vector>
12 #include <utility>
13 #include <iomanip>
14 #include <string>
15 #include <cmath>
16 #include <queue>
17 #include <assert.h>
18 #include <map>
19 #include <ctime>
20 #include <cstdlib>
21 #include <stack>
22 #define LOCAL
23 const int MAXN = 1000000 + 10;
24 const int INF = 100000000;
25 const int SIZE = 450;
26 const int MAXM = 1000000 + 10;
27 const int maxnode =  0x7fffffff + 10;
28 using namespace std;
29 int l1, l2;
30 char a[MAXN], b[MAXN];
31 int next[20000];//不用开太大了..
32 void getNext(){
33      //初始化next数组
34      next[1] = 0;
35      int j = 0;
36      for (int i = 2; i <= l1; i++){
37          while (j > 0 && a[j + 1] != a[i]) j = next[j];
38          if (a[j + 1] == a[i]) j++;
39          next[i] = j;
40      }
41      return;
42 }
43 int kmp(){
44     int j = 0, cnt = 0;
45     for (int i = 1; i <= l2; i++){
46         while (j > 0 && a[j + 1] != b[i]) j = next[j];
47         if (a[j + 1] == b[i]) j++;
48         if (j == l1){
49            cnt++;
50            j = next[j];//回到上一个匹配点
51         }
52     }
53     return cnt;
54 }
55
56 void init(){
57      scanf("%s", a + 1);
58      scanf("%s", b + 1);
59      l1 = strlen(a + 1);
60      l2 = strlen(b + 1);
61 }
62
63 int main(){
64     int T;
65
66     scanf("%d", &T);
67     while (T--){
68           init();
69           getNext();
70           printf("%d\n", kmp());
71     }
72     /*scanf("%s", a + 1);
73     l1 = strlen(a + 1);
74     getNext();
75     for (int i = 1; i <= l1; i++) printf("%d ", next[i]);*/
76     return 0;
77 }

时间: 2024-12-23 18:11:31

【POJ3461】【KMP】Oulipo的相关文章

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

【洛谷】3375 KMP字符串匹配

[算法]KMP [题解][算法]字符串 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=1000010,maxm=1010; char A[maxn],B[maxm]; int p[maxm],n,m; int main() { scanf("%s%s",A+1,B+1); n=strlen(A+1);m=strlen

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

【KMP】【最小表示法】NCPC 2014 H clock pictures

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个针都是相同的,分别指向Ai,Bi(360°被分成360000小份),问能否将其中一个旋转和另一个重合. 题目思路: [KMP][最小表示法] 循环同构问题.可以写KMP,我懒得写KMP了就写了循环同构的最小表示法. 首先将Ai排序,然后求差(记得取模360000,WA了一次),接下来复制一遍开始匹配. A

HDOJ3336 Count the string 【KMP前缀数组】+【动态规划】

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4212    Accepted Submission(s): 1962 Problem Description It is well known that AekdyCoin is good at string problems as well as n

【动态规划】【KMP】HDU 5763 Another Meaning

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成2种意思,问s1可以解读成几种意思(mod 1000000007). 题目思路: [动态规划][KMP] 题目有点绕,看看样例就懂了.其实不用KMP直接用substr就能做. 首先不解读成另一个意思的话,f[i]=f[i-1],接着如果当前位置能够与s2匹配,那么f[i]+=f[i-strlen(s2)]

HDU3746 Cyclic Nacklace 【KMP】

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2538    Accepted Submission(s): 1154 Problem Description CC always becomes very depressed at the end of this month, he has checke

NYOJ327 亲和串 【KMP】

亲和串 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 最近zyc遇到了一个很棘手的问题:判断亲和串,以前判断亲和串的时候直接可以看出来,但现在不同了,现在给出的两字符串都非常的大,看的zyc头都晕了.于是zyc希望大家能帮他想一个办法来快速判断亲和串.亲和串定义:给定两个字符串s1和s2,如果能通过s1循环移动,使s2包含在s1中,那么我们就说s2是s1的亲和串. 输入 本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的

HDU4763 Theme Section 【KMP】

Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1114    Accepted Submission(s): 579 Problem Description It's time for music! A lot of popular musicians are invited to join us in t

HDU2594 Simpsons’ Hidden Talents 【KMP】

Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2798    Accepted Submission(s): 1055 Problem Description Homer: Marge, I just figured out a way to discover some of the