POJ3461题解——KMP入门

题目链接:http://poj.org/problem?id=3461

Oulipo

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
题目意思:就是先给你一个整数t表示有多少组样例,每组样例由两个字符串组成,找出第一个字符串在第二个字符串中出现的次数。
题目思路:这不就是kmp算法的模板题嘛。只不过我们按照kmp的规则去匹配当匹配完成以后(j==plen时)我们要做的不是返回模式串在文本串的位置,而是count++计数,并且继续往后找有没有再次和模式串匹配相同的,这里我们让j=next[j],而这也恰好是失配时的做法。
ac代码如下:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=1e6+10;
int n,count,next[maxn];
char s[maxn],p[maxn];
void GetNext(char *p)
{
    int j=-1;
    int i=0;
    next[0]=-1;
    int plen=strlen(p);
    while(i<plen)
    {
        if(j==-1||p[i]==p[j])
        {
            ++i;++j;
            if(p[i]==p[j])next[i]=next[j];
            else next[i]=j;
        }
        else j=next[j];
    }
}
void kmp(char *p,char *s)
{
    int plen=strlen(p);
    int slen=strlen(s);
    
    int i=0;
    int j=0;
    
    while(i<slen&&j<plen)
    {
        if(j==-1||s[i]==p[j])
        {
            ++i;++j;
        }
        else j=next[j];
        if(j==plen)
        {
            count++;
            j=next[j];
        }
        //printf("i=%d j=%d\n",i,j);
    }
    return;
}
int main()
{
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s%s",p,s);
        count=0;
        GetNext(p);
        /*
        for(int i=0;i<strlen(p);i++)
        printf("%d ",next[i]);printf("\n");
        */
        kmp(p,s);//printf("666\n");
        printf("%d\n",count);
    }
    return 0;
}
View Code

 

posted @ 2019-11-05 09:42  Maynerd  阅读(185)  评论(0编辑  收藏  举报