POJ 3461 kmp, poj1_1kmp

Source: Internet
Author: User

POJ 3461 kmp, poj1_1kmp
Oulipo

Time Limit:1000 MS   Memory Limit:65536 K
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 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

Source

BAPC 2006 Qualification inexplicable 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", a, B); 55 la = strlen (a); lb = strlen (B); 56 makep (); 57 KMP (); 58} 59 60 61 return 0; 62}TLE

 

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", B, a); 46 la = strlen (a); 47 lb = strlen (B); 48 getnext (); 49 printf ("% d \ n", kmp (); 50} 51}AC

 

Related Article

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.