Click here to transfer POJ 3461
**oulipo**the French author Georges Perec (1936–1982) once wrote a book, La disparition,without theLetter ' E '. He was a member of theOulipo Group. AQuote from theBook: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 (orrather, low)inch theFollowing contest. People is asked to WriteA perhaps even meaningfultext on someSubject with asFew occurrences ofAgiven“Word” asPossible. Our task is toProvide theJury withA program thatCounts these occurrences,inchOrder toObtain a ranking of theCompetitors. These competitors oftenWriteVery long texts withNonsense meaning; A sequence of -, theConsecutive ' T ' s is notUnusual. And they never use spaces. So we want toQuickly find out how often aWord, i.e., agiven string, occursinchAtext. More formally:given theAlphabet {' A ', ' B ', ' C ', ..., ' Z '} andFinite strings Over thatAlphabet, aWordW andAtextTCount the Number ofOccurrences ofWinchT. All theConsecutivecharacters ofW must exactly match consecutivecharacters ofT. Occurrences may overlap. Inputthe FirstLine of theInputfile containsA single Number: the Number ofTest Cases toFollow. Each test case has theFollowing Format:one line with the WordW, astring Over{' A ', ' B ', ' C ', ..., ' Z '}, with 1≤| w| ≤Ten, the(Here | w| Denotes the length of the stringW). One line with the textT, astring Over{' A ', ' B ', ' C ', ..., ' Z '}, with| w| ≤| T| ≤1, the,.OutputforeveryTest caseinch theInputfile, theOutput shouldcontainA single Number, onA single line: the Number ofOccurrences of the WordWinch the textT.sample Input3Bapcbapcazaazazazaverdiaverdxivyerdiansample Output130
The main topic: is to give you a substring p and a main string s, how many substrings in the primary string ....
Problem-solving ideas: These days have been in the whole AC automata, just start a look reflex I think is AC automata, the result is not, because, AC automata are to you a lot of strings, let you look for the prefix, this is not, this is 22 compared so is obviously KMP, the result is ok .... But at first, I made a mistake, and I'll give you an introduction later.
On the code:
Here is the AC code:
#include <iostream>#include <cstdio>#include <cstring>using namespace STD;Const intMAXN =1000005;CharS[MAXN], P[MAXN];intNEXT[MAXN];intans;voidGetnext () {intj =0;intK =-1; next[0] = -1;intPlen =strlen(P); while(J < Plen) {if(k==-1|| P[j]==p[k]) {k++; j + +; NEXT[J] = k; }Elsek = Next[k]; }}intKMP () {inti =0;intj =0; Getnext ();intSlen =strlen(S);intPlen =strlen(P); while(I<slen && J<plen) {if(j==-1|| S[i]==p[j]) {i++; j + +; }Elsej = Next[j];if(j = = Plen) {ans++; j = Next[j]; } }returnAns;}intMain () {intTscanf("%d", &t); while(t--) {ans =0;scanf("%s%s", p,s); KMP ();printf("%d\n", ans); }return 0;}
Below gives a tle code, is not feeling with the front almost the same, please look for the bug, in fact, this is also an experience ah, said more are tears ah ... ;
#include <iostream>#include <cstdio>#include <cstring>using namespace STD;Const intMAXN =1000005;CharS[MAXN], P[MAXN];intNEXT[MAXN];intans;voidGetnext () {intj =0;intK =-1; next[0] = -1; while(J <strlen(P)) {if(k==-1|| P[j]==p[k]) {k++; j + +; NEXT[J] = k; }Elsek = Next[k]; }return;}intKMP () {inti =0;intj =0; Getnext (); while(i<strlen(S) && j<strlen(P)) {if(j==-1|| S[i]==p[j]) {i++; j + +; }Elsej = Next[j];if(J = =strlen(P)) {ans++; j = Next[j]; } }returnAns;}intMain () {intTscanf("%d", &t); while(t--) {ans =0;scanf("%s%s", p,s); KMP ();printf("%d\n", ans); }return 0;}
Unlike the previous is in the length of the calculation string, if it has been in the while loop, it will always count, will be tle, so directly in the outside forget, next time must pay attention, there should be no next time ...
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Poj 3461 Oulipo