Oulipo
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 36424 |
|
Accepted: 14721 |
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 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 ...
The
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 ' s 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's 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.
Input
The first line of the input file contains a, 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.
Output
For every test case in the input file, the output should contain a single number, and on a single line:the number of Occurren Ces of the word W in the text T.
Sample Input
3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIAN
Sample Output
130
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:
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 ...
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
/*KMP Algorithm*/#include<cstdio>#include<cstring>using namespacestd;#defineN 2100010intT,lens,lenp,next[n];CharS[n],p[n];voidGet_next () {intI=0, j=-1; Next[i]=J; while(i<LENP) { if(j==-1|| p[i]==P[j]) {i++;j++; Next[i]=J; } Elsej=Next[j]; }}intKMP () {get_next (); intI=0, j=0, ans=0; while(i<lens&&j<LENP) { if(j==-1|| s[i]==P[j]) {i++;j++; } Elsej=Next[j]; if(J==LENP) ans++,j=Next[j]; } returnans;}intMain () {//KMP The number of occurrences of child stringsscanf"%d",&T); while(t--) {scanf ("%s%s", P,s); Lens=strlen (s); LENP=strlen (P); printf ("%d\n", KMP ()); } return 0;}
POJ 3461 Oulipo