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 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 would 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 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 single 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, on a single line: the number of occurrences of the word W in the text T.
Sample Input
3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN
Sample Output
1 3 0
这道题采用了KMP算法,关于KMP算法的具体解释,请看这些博客:http://billhoo.blog.51cto.com/2337751/411486,http://blog.csdn.net/yaochunnian/article/details/7059486,http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html
他们讲的非常详细,KMP算法理解起来非常困难,需要潜下心来,好好仔细研究研究,没几个小时是不能真正理解其中的意义的。
/*
*POJ 3461
*KMP 算法
*提示:代码提交选择C++编译器。G++编译器提交超时。
*/
#include <iostream>
#include <cstring>
using namespace std;
#define maxw 10005
#define maxt 1000005
void getnext(char *p, int *next)
{
int j, l, cur;
next[0] = -1; //设置w的前缀函数suffix[]的边界值
next[1] = 0;
j = 0;
l = strlen(p);
for( cur = 2; cur <= l; cur++ )
{
while( j >= 0 && p[j] != p[cur - 1] ) //应用kmp算法计算单词w的前缀函数
j = next[j];
next[cur] = ++j;
}
}
int match( char *s, char *p, int *next) //s为搜索串 p为匹配串 next为前缀函数
{
int i, j, slen, plen, cnt;
i = 0; j = 0; cnt = 0;
slen = strlen(s);
plen = strlen(p);
while( i < slen )
{
if( j == -1 || s[i] == p[j] ) //若s和w的指针指向当前的字符相同,则同时向右移动一个位置,或者分析完了所有p的字符后
//s的下一个字符与p的第一个字符进行匹配
i++, j++;
else if( j >= 0 )
j = next[j]; //消除了指针i的回溯。若未分析完p的所有字符,则根据next数组的值左移p的指针
//否则s的下一个字符与p的第一个字符匹配
if( j == plen )
cnt++, j = next[j];
}
return cnt;
}
int main()
{
int n;
char w[maxw], t[maxt];
int suffix[maxw];
cin >> n;
while( n -- )
{
cin >> w >> t;
getnext( w, suffix );
cout << match( t, w, suffix) << endl;
}
}