Oulipo
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:22295 |
|
Accepted:8905 |
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 ù s' 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
It's a strange thing. Recently I thought it was very smooth when I was doing the problem coding, But I found all kinds of problems during debugging. Today, I found the vulnerability for a long time in line 31st >_<|| | meaning: specify the number of mode strings (including overlapping) in a mode string and a master string ). Question: This is the KMP entry question. You only need to scan left to right to obtain the answer. The meaning of the next array is: if the J character of the mode string does not match the I character of the Main string, the J character of the mode string must be traced back to a position and re-matched with the I character of the Main string, next is used to record the Backtracking position,
#include <stdio.h>#define maxn 10002#define maxN 1000002char str1[maxN], str2[maxn];int next[maxn];void getNext(){int i = 0, j = -1;next[0] = -1;while(str2[i]){if(j == -1 || str2[i] == str2[j]){++i; ++j;if(str2[i] == str2[j]) next[i] = next[j];else next[i] = j;}else j = next[j];}}int KMP(){int ans = 0; getNext();int i = 0, j = 0;while(str1[i]){if(j == -1 || str1[i] == str2[j]){++i; ++j;}else j = next[j];if(j != -1 && !str2[j]){ //Attention!!!don't forget "j != -1"!++ans; j = next[j];}}return ans;}int main(){//freopen("stdin.txt", "r", stdin);int t;scanf("%d", &t);while(t--){scanf("%s%s", str2, str1);printf("%d\n", KMP());}return 0;}