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算法是一种改进的字符串匹配算法,KMP算法的核心是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的。具体实现就是通过一个next()函数实现,函数本身包含了模式串的局部匹配信息。 ——来自百度百科
KMP算法主要分为两部分,一:求next数组,二:字符串匹配
代码:
#include<stdio.h>
#include<string.h>
char a[1000010],b[1000010];
int nx[1000010];
int l1,l2;
void get_next(char s[],int l) //s是模式串
{
nx[0]=-1; /*方便判断是否越界与判断第0位和第i位*/
for(int i=1,j=0; i<=l;) /*i的下标从开始,循环到模式串的末尾,寻找最长公共前后缀*/
{
nx[i]=j;
while(j>=0&&s[j]!=s[i]) /*更新j的值,若不符合条件,则不进行while循环直接++*/
j=nx[j];
j++,i++;
}
}
int kmp(char s1[],char s2[]) //s1是模式串,s2是主串
{
int s=0;
l1=strlen(s1);
l2=strlen(s2);
get_next(s1,l1); /*求模式串的next数组*/
for(int i=0,j=0; i<l2;)
{
while(j>=0&&s1[j]!=s2[i]) /*模式串与主串进行匹配*/
j=nx[j];
j++,i++;
if(j==l1) /*如果模式串下标等于模式串长度时即匹配成功*/
{
s++;
j=nx[j]; /*每次匹配成功后更新j的值*/
}
}
return s;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s %s",a,b);
printf("%d\n",kmp(a,b)); /*注意这里模式串与主串的位置要和调用的函数一样,容易写反*/
}
return 0;
}
上面的代码这个模板不太好,记的话记下面这个模板 注意下面这个代码不是上面题对应的代码,只是存一个模板而已
题目:POJ-2406
KMP算法模板
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[1000010];
int nx[1000010];
void get_next(int l)
{
int i=0,j=-1;
nx[0]=-1;
while(i<l)
{
if(j==-1||a[i]==a[j])
{
i++,j++;
nx[i]=j;
}
else
j=nx[j];
}
}
int main()
{
while(~scanf("%s",a)&&a[0]!='.')
{
int l1=strlen(a);
get_next(l1);
int l2=l1-nx[l1];
if(l1%l2==0)
printf("%d\n",l1/l2);
else
printf("1\n");
}
return 0;
}