POJ 3461  Oulipo

来源:互联网 发布:深圳做seo哪家公司好 编辑:程序博客网 时间:2024/06/06 11:47
Oulipo
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 8508Accepted: 3338

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 avaitFair normal, d’abord, puis surgissait l’inhumain, l’affolant. Ilaurait voulu savoir où s’articulait l’association qui l’unissait auroman : stir son tapis, assaillant à tout instant son imagination,l’intuition d’un tabou, la vision d’un mal obscur, d’un quoivacant, d’un non-dit : la vision, l’avision d’un oubli commandanttout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in thefollowing contest. People are asked to write a perhaps evenmeaningful text on some subject with as few occurrences of a given“word” as possible. Our task is to provide the jury with a programthat counts these occurrences, in order to obtain a ranking of thecompetitors. These competitors often write very long texts withnonsense meaning; a sequence of 500,000consecutive 'T's is not unusual. And theynever use spaces.

So we want to quickly find out how often a word, i.e., a givenstring, occurs in a text. More formally: given the alphabet{'A''B''C',…, 'Z'} and two finite strings over thatalphabet, a word W and atext T, count the number of occurrencesof W in T.All the consecutive characters of W must exactly match consecutivecharacters of T. Occurrences mayoverlap.

Input

The first line of the input file contains a single number: thenumber of test cases to follow. Each test case has the followingformat:

  • One line with the word W, a string over{'A''B''C',…, 'Z'}, with 1 ≤ |W| ≤ 10,000(here |W| denotes the length of thestring 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 containa single number, on a single line: the number of occurrences of theword W in thetext T.

Sample Input

BAPC 
BAPC 
AZA 
AZAZAZA 
VERDI 
AVERDXIVYERDIAN

Sample Output

0

Source

BAPC 2006Qualification
KMP,基本没弄明白,只是照着学长的自己敲了一遍
代码:
C语言临时自用代码
#include<stdio.h>
#define M100010
char s[M],t[M];
int next[M],sum;
void getNext()//求next数组
{
    int i,j;
    next[0]=-1;
    for(i=1,j=-1;s[i];i++){
        while(j!=-1&&s[i]!=s[j+1])
            j=next[j];//从s[j+1]开始找与s[i]相同的字母
        if(s[j+1]==s[i])
            j++;
        next[i]=j;//如果找到相同字母,next[i]记录此位置,否则next[i]=next[i-1]
    }
}
void kmp()
{
    int i,j;
    sum=0;
    getNext();
    for(i=0,j=-1;t[i];i++){
        while(j!=-1&&s[j+1]!=t[i])
            j=next[j];//按next[j]后退找出与t[i]相同的s[j+1]
        if(s[j+1]==t[i])
            j++;//如果找到则向后前进
        if(!s[j+1]){//如果在t中找到完整的s
            sum++;//计数增1
            j=next[j];//按next后退
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%s%s",s,t);
        kmp();
        printf("%d\n",sum);
    }
    return 0;
}
原创粉丝点击