POJ 3461 kmp

Oulipo

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 40168

Accepted: 16135

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

莫名其妙TLE

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 char a[10001];
 6 char b[1000001];
 7 int  p[10001];
 8 int la,lb;
 9 int j;
10 int ans;
11 inline void makep()
12 {
13     
14     j=0;
15     for(int i=1;i<la;i++)
16     {
17         while(j>0&&a[i]!=a[j])
18         j=p[j];
19         if(a[i]==a[j])
20         j++;
21         p[i]=j;
22     }
23     return ;
24 }
25 inline void KMP()
26 {
27     j=0;
28     ans=0;
29     for(int i=0;i<lb;i++)
30     {
31         while(b[i]!=a[j]&&j>0)
32         j=p[j-1];
33         if(b[i]==a[j])
34         j++;
35         if(j==la)
36         {
37             ans++;
38             j=p[j-1];        
39         }
40         
41     }
42     printf("%d\n",ans);
43     return ;
44 }
45 
46 int main()
47 {
48     int n;
49     scanf("%d",&n);
50     
51     for(int i=1;i<=n;i++)
52     {
53         memset(p,0,sizeof(p));
54         scanf("%s%s",a,b);
55         la=strlen(a);lb=strlen(b);
56         makep();
57         KMP();
58     }
59         
60     
61     return 0;
62 }

AC

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 using namespace std;
 6 
 7 int n,la,lb;
 8 int next[100010];
 9 char a[1000010],b[100010];
10 
11 void getnext()
12 {
13     int j=-1;
14     next[0]=-1;
15     for(int i=1;i<lb;i++)
16     {
17         if(j!=-1 && b[j+1]!=b[i])    j=next[j];
18         if(b[j+1]==b[i])    j++;
19         next[i]=j;
20     }
21 }
22 
23 int kmp()
24 {
25     int ans=0;
26     int j=-1;
27     for(int i=0;i<la;i++)
28     {
29         if(j!=-1 && a[i]!=b[j+1])    j=next[j];
30         if(b[j+1]==a[i])    j++;
31         if(j==lb-1)    
32         {
33             ans++;    
34             j=next[j];
35         }    
36     }
37     return ans;
38 }
39 
40 int main()
41 {
42     scanf("%d",&n);
43     for(int u=1;u<=n;u++)
44     {
45         scanf("%s %s",b,a);
46         la=strlen(a);
47         lb=strlen(b);
48         getnext();
49         printf("%d\n",kmp());
50     }
51 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • HDU 3032 Nim or not Nim?(Multi-Nim)

    Problem Description Nim is a two-player mathematic game of strategy in which ...

    attack
  • P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛....

    attack
  • HDU 1506 Largest Rectangle in a Histogram(单调栈)

    attack
  • HDU 3032 Nim or not Nim?(Multi-Nim)

    Problem Description Nim is a two-player mathematic game of strategy in which ...

    attack
  • HDUOJ----2489 Minimal Ratio Tree

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768...

    Gxjun
  • 【Codeforces】1213B - Bad Prices

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

    喜欢ctrl的cxk
  • Codeforces 626E Simple Skewness(暴力枚举+二分)

    E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megab...

    Angel_Kitty
  • AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)

    A. Diversity time limit per test:1 second memory limit per test:256 megabytes in...

    Angel_Kitty
  • CodeFores 665D Simple Subset(贪心)

    D. Simple Subset time limit per test 1 second memory limit per test 256 me...

    ShenduCC
  • 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛- A. Banana

    In the forest, there is a Banana Company that provides bananas from different pl...

    Enterprise_

扫码关注云+社区

领取腾讯云代金券