1570. [POJ3461] Ulipo
★☆ input file: oulipo.in output file: oulipo.out simple comparison
Time limit: 1 s memory limit:
"Title description"
French writer George Peyrec (Georges perec,1936-1982) once wrote a book, "The Sensitive Letter" (La disparition), without a letter ' E '. He is a member of the Ulipo Group (Oulipo group). Here is a passage from his book:
toutavait 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 ' articulaitl ' association qui l ' unissait au roman:stir son tapis, assaillantàtoutinstant 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 commandanttout, oùs ' abolissait la raison:tout avait l ' air normal mais ...
Pereque is likely to get a high score in the following races (and, of course, it could be a low score). In this race, people are asked to write even meaningful articles about a topic and to make a given word appear as few times as possible. Our task is to write a program for the jury to count the number of words appearing several times in order to draw the final rank of the contestants. Participants often write a long list of nonsense, such as 500,000 consecutive ' T '. And they don't have spaces.
So we want to find the frequency that a word appears as soon as possible, that is, a given string appears several times in the article. More formally, give the alphabet {' A ', ' B ', ' C ',..., ' Z '} and two finite strings with only letters in the alphabet: the word w and the article T, and find the number of times that W appears in T. This "presence" means that all successive characters in W must correspond to successive characters in T. The two Watts that appear in t may partially overlap.
"Input Format"
The input contains multiple sets of data.
The first line of the input file has an integer that represents the number of data groups. The following is the data, given in the following format:
The first line is the word w, a string consisting of the letters {' A ', ' B ', ' C ',..., ' Z '}, guaranteeing 1<=| w|<=10000 (| W| represents the length of the string W)
The second line is the article T, a string consisting of the letters {' A ', ' B ', ' C ',..., ' Z '}, Guaranteed | w|<=| t|<=1000000.
"Output Format"
One integer for each set of data output, that is, the number of times W appears in T.
"Sample Input"
3
Bapc
Bapc
AZA
Azazaza
VERDI
Averdxivyerdian
"Sample Output"
1
3
0
1#include <iostream>//KMP2#include <cstring>3#include <cstdio>4 5 #defineW 10000+106 #defineT 1000000+107 8 using namespacestd;9 Ten CharW[w],t[t]; One intN,fail[w]; A intL1,l2; - intsum=0; - the voidFail (); - voiddoit (); - - intMain () + { -Freopen ("oulipo.in","R", stdin); +Freopen ("Oulipo.out","W", stdout); Ascanf"%d",&n); at while(n--) - { -Memset (W,0,sizeof(w)); -memset (T,0,sizeof(t)); -memset (fail,0,sizeof(fail)); -sum=0; inscanf"%s%s", w,t); - Fail ();d oit (); toprintf"%d\n", sum); + } - fclose (stdin); fclose (stdout); the return 0; * } $ voidFail ()//creating a Fail arrayPanax Notoginseng { -fail[0]=0; thel1=strlen (w); +L2=strlen (t); A for(intj=0, i=1; i<l1;i++) the { + while(J&&w[j]!=w[i]) j=fail[j-1]; - if(W[j]==w[i]) J + +; $fail[i]=J; $ } - } - voidDoit ()//Search the number of Word occurrences the { - intj=0;Wuyi for(intI=0; i<l2;++i) the { - while(j&&w[j]!=T[i]) Wuj=fail[j-1]; - if(W[j]==t[i]) J + +; About if(j==L1) $ { -sum++; -j=fail[j-1]; - } A } +}
Kmp--cogs 1570 Ulipo