OulipoTime limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 9731 Accepted Submission (s): 3859
Problem DescriptionThe 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 s On 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 has scored high (or rather, low) in the following contest. People is asked to write a perhaps even meaningful text on some subject with as few occurrences of a given "word" as poss Ible. Our tasks are to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the Competit Ors. These competitors often write very long texts with nonsense meaning; A sequence of 500,000 consecutive ' T ' is not unusual. And they never use spaces.
So we want to quickly find out what often a word, i.e., a given string, occurs in a text. More Formally:given the alphabet {' A ', ' B ', ' C ', ..., ' Z '} and both 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.
Inputthe 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.
Outputfor every test case in the input file, the output should contain a single number, on a single line:the number of OC Currences of the word W in the text T.
Sample Input
3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIAN
Sample Output
130
Test instructions: Find out how many pattern strings are in the text string.
KMP each time J traverses to M-1 to indicate a successful match, the next match must start with the current I and Next[j]
, and then continue to match on it.
I don't know why hash is too hard ~
#include <cstring> #include <iostream> #include <cstdio> #include <algorithm> #include < Vector> #include <cmath>using namespace std; #define MAXN 1111111char P[MAXN], T[maxn];int N, m; #define Next Nexti NT Next[maxn];void Get_next (char *p) { int t; t = next[0] =-1; int j = 0; while (J+1 < m) { if (T < 0 | | p[j] = = P[t]) {//matching j + +, t++; NEXT[J] = t; } else//mismatch t = next[t]; } } int KMP () {int ans = 0;get_next (P); int i = 0, j = 0;while (I < n && J < m) {if (J < 0 | | T[i] = = P[j]) {if (j = = m-1) {ans++;j = next[j];continue;} i++, j + +;} else {j = next[j];}} return ans;} int main () {int cas;cin >> cas;while (cas--) {scanf ("%s%s", p, t), n = strlen (t), M = strlen (P);p rintf ("%d\n", km P ());} return 0;}
HDU 1686 (KMP)