KMP入门例题(POJ3461,HDU1711)

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

这里推荐先去多看些别的视频以及资料啥的,注意区分一下,最长前缀后缀数组和next数组的区别(其实差不了多少,next数组是为了方便操作下标,最长前缀后缀数组全体右移1位并减一,然后第一位赋值为-1就是next数组,当然你直接把最长前缀后缀数组当成next数组也行,这里分享的代码就是这样);

例题1:POJ3461

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[1020000];
char des[1020000];
int Next[12000];
int ans;
void make_next(const char des[],int next[]){
	int i,j; //i:des的下标 ,j :最长公共前后缀长度
	next[0]=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;
}

例题2:HDU1711(裸模板)

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[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , 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[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , 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[1000020];
int des[10020];
int Next[10020];
int n,m;
void make_next(const int des[],int next[]){
	int i,j;
	next[0]=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;
}

之看代码,看不出什么所以然的,先去理解概念~~

猜你喜欢

转载自blog.csdn.net/qq_40922859/article/details/81812818
今日推荐