Poj 3461 oulipo (KMP string matching)

Source: Internet
Author: User

Here are two strings, P and S. Calculate the number of times P appears in S. The KMP is bare.

Because we need to loop through the J = next [J] process to know the end point of s more than once.

#include<cstdio>#include<cstring>using namespace std;const int N = 10005, M = 1000005;int next[N], ans, n;char p[N], s[M];void kmp (){    int lp = strlen (p), ls = strlen (s), i, j;    next[0] = -1, next[1] = i = 0;    while ( (++i) < lp)    {        j = next[i];        while (j != -1 && p[i] != p[j])            j = next[j];        next[i + 1] = j + 1;    }    i = j = 0;    while (i < ls)    {        if (j == -1 || s[i] == p[j])        {            ++i, ++j;            if (j == lp) ++ans, j = next[j];        }        else j = next[j];    }}int main(){    scanf ("%d", &n);    while (n--)    {        ans = 0;        scanf ("%s%s", p, s);        kmp ();        printf ("%d\n", ans);    }    return 0;}

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 locale 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 wowould 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 wordWAnd a textT, Count the number of occurrencesWInT. All the consecutive characters of W must exactly match consecutive charactersT. 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 wordW, A string over {'A','B','C',...,'Z'}, With 1 ≤ |W| ≤ 10,000 (here |W| Denotes the length of the stringW).
  • One line with the textT, A string over {'A','B','C',...,'Z'}, With |W| ≤|T| ≤ 1,000,000.

Output

For every test case in the input file, the output shoshould contain a single number, on a single line: the number of occurrences of the wordWIn the textT.

Sample Input

3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIAN

Sample output

130


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.