# KMP入门例题（POJ3461,HDU1711）

KMP算法比较难理解，这里推荐一个视频：哔哩哔哩~阿三大神

Oulipo

 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 49336 Accepted: 19597

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

Source

BAPC 2006 Qualification

#include<iostream>
#include<cstring>
using namespace std;
char src;
char des;
int Next;
int ans;
void make_next(const char des[],int next[]){
int i,j; //i：des的下标 ，j :最长公共前后缀长度
next=0;
int m=strlen(des);
for(i=1,j=0;i<m;i++){   //i从1(第二个)开始
while(j>0 && des[i]!= des[j])  //递归的往前求
j=next[j-1];
if(des[i]==des[j]){  //相等，最长公共前后缀长度加一
j++;
}
next[i]=j;
}
}
void kmp(const char src[],const char des[],int next[]){
int n=strlen(src);
int m=strlen(des);
int i,j;
make_next(des,next);
for(i=0,j=0;i<n;i++){
while(j>0&&des[j]!=src[i])
j=next[j-1];
if(des[j]==src[i]){
j++;
}
if(j==m){
ans++;
j=next[j-1];
// j=0;    //如果不找重叠的串，就直接重置j为0；
}
}
return ;
}
int main(){

int t;
scanf("%d",&t);

while(t--){
scanf("%s%s",&des,&src);
ans=0;
kmp(src,des,Next);
printf("%d\n",ans);
}
return 0;
}


# Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 40381    Accepted Submission(s): 16654

Problem Description

Given two sequences of numbers : a, a, ...... , a[N], and b, b, ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b, a[K + 1] = b, ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.

Input

The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a, a, ...... , a[N]. The third line contains M integers which indicate b, b, ...... , b[M]. All integers are in the range of [-1000000, 1000000].

Output

For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.

Sample Input


 

2

13 5

1 2 1 2 3 1 2 3 1 3 2 1 2

1 2 3 1 3

13 5

1 2 1 2 3 1 2 3 1 3 2 1 2

1 2 3 2 1

Sample Output


 

6

-1

Source

HDU 2007-Spring Programming Contest

#include<iostream>
#include<cstring>
using namespace std;
int src;
int des;
int Next;
int n,m;
void make_next(const int des[],int next[]){
int i,j;
next=0;
for(i=1,j=0;i<m;i++){
while(j>0 && des[i]!= des[j])
j=next[j-1];
if(des[i]==des[j]){
j++;
}
next[i]=j;
}
}
int kmp(const int src[],const int des[],int next[]){

int i,j;
make_next(des,next);
for(i=0,j=0;i<n;i++){
while(j>0&&des[j]!=src[i])
j=next[j-1];
if(des[j]==src[i]){
j++;
}
if(j==m){
return i-m+2;
}
}
return -1;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%d",&src[i]);
}
for(int j=0;j<m;j++){
scanf("%d",&des[j]);
}
int ans=kmp(src,des,Next);
printf("%d\n",ans);
}

return 0;
}

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客 