Oulipo
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:17128 |
|
Accepted:6892 |
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 locale 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 ipvs '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
Question: Give you two strings and find the number of times the first string appears in the second string.
Solution: Find Next in the first string and then match it with the second string. The difference between the Cut cloth and the Cut cloth is that this can overlap.
Address: oulipo
AC code:
# Include <iostream> # include <cstring> # include <string> # include <cstdio> using namespace STD; char a [10005], p [1000005]; int next [10005], res, len1, len2; void getnext () {int I, j; next [0] = 0; next [1] = 0; for (I = 1; I <len1; I ++) {J = next [I]; while (J & A [I]! = A [J]) J = next [J]; if (a [I] = A [J]) next [I + 1] = J + 1; else next [I + 1] = 0 ;}} void KMP () {int I, j = 0; for (I = 0; I <len2; I ++) {While (J & P [I]! = A [J]) J = next [J]; If (P [I] = A [J]) J ++; If (j = len1) {J = next [J]; // It can overlap when matching. res ++ ;}}int main () {int N; CIN >>n; while (n --) {scanf ("% S % s", A, P ); res = 0; len1 = strlen (A), len2 = strlen (p); getnext (); KMP (); printf ("% d \ n", Res );} return 0 ;}