Poj 3461 oulipo (simple KMP)

Source: Internet
Author: User
Oulipo
Time limit:1000 ms   Memory limit:65536 K
Total submissions:17128   Accepted:6892

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 ipvs '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
Question: Give you two strings and find the number of times the first string appears in the second string.
Solution: Find Next in the first string and then match it with the second string. The difference between the Cut cloth and the Cut cloth is that this can overlap.
Address: oulipo
AC code:
# Include <iostream> # include <cstring> # include <string> # include <cstdio> using namespace STD; char a [10005], p [1000005]; int next [10005], res, len1, len2; void getnext () {int I, j; next [0] = 0; next [1] = 0; for (I = 1; I <len1; I ++) {J = next [I]; while (J & A [I]! = A [J]) J = next [J]; if (a [I] = A [J]) next [I + 1] = J + 1; else next [I + 1] = 0 ;}} void KMP () {int I, j = 0; for (I = 0; I <len2; I ++) {While (J & P [I]! = A [J]) J = next [J]; If (P [I] = A [J]) J ++; If (j = len1) {J = next [J]; // It can overlap when matching. res ++ ;}}int main () {int N; CIN >>n; while (n --) {scanf ("% S % s", A, P ); res = 0; len1 = strlen (A), len2 = strlen (p); getnext (); KMP (); printf ("% d \ n", Res );} return 0 ;}

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.