今天看了很久的KMP, 终于看懂原理了, 然后网上代码大部分比较迷, 而且风格各异, 为了方便大家, 下面将贴上几个KMP的模板,建议还是先看懂原理(推荐《算法导论》-把整章看完 || 博客:http://blog.csdn.net/v_july_v/article/details/7041827 ,都讲得十分详细);
代码1:HDU1711
**Number Sequence**
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25051 Accepted Submission(s): 10606
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
题目大意: 给两个数列a[]、b[], 求b[]在a[]中第一次出现的位置, 若没有输出-1
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#define For(a, b, c) for(a = b; a <= c; ++a)
using namespace std;
const int N = 1e6 + 10;
int ans, a, b;
int pi[10005], P[10005], T[1000005];
void pre(){
int q = 0, k;
scanf("%d%d", &a, &b);
For(k, 0, a - 1) scanf("%d", &T[k]);
For(k, 0, b - 1) scanf("%d", &P[k]);
k = pi[0] = -1;
while(q < b){
while(k != -1 && P[k] != P[q]) k = pi[k];
pi[++q] = ++k;
}
}
void KMP(){
pre();//自配
int q, k = 0;
For(q, 0, a - 1){
while(k != -1 && T[q] != P[k]) k = pi[k];
if(++k == b){//达到目标状态->匹配成功
ans = q;
return ;
}
}
}
void init(){
memset(pi, 0, sizeof(pi));
ans = 0;
}
int main(){
int t;
scanf("%d", &t);
while(t--){
init();
KMP();
if(ans) printf("%d\n", ans - b + 2);
else printf("-1\n");
}
return 0;
}
代码2: POJ3461
Oulipo
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 39227 Accepted: 15792
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
题目大意: 给两个字符串T,P,求P在T中出现的次数。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#define For(a, b, c) for(a = b; a <= c; ++a)
using namespace std;
const int N = 1e6 + 10;
int pi[N], ans;
char T[N], P[N];
void pre(){
int q = 0, k;
int m = strlen(P);
k = pi[0] = -1;
while(q < m){
while(k != -1 && P[k] != P[q]) k = pi[k];
pi[++q] = ++k;
}
}
void KMP(){
pre();
int n = strlen(T), m = strlen(P);
int q, k = 0;
For(q, 0, n - 1){
while(k != -1 && T[q] != P[k]) k = pi[k];
if(++k >= m) ++ans, k = pi[k];
}
}
void init(){
memset(pi, 0, sizeof(pi));
ans = 0;
}
int main(){
int t;
scanf("%d", &t);
while(t--){
init();
scanf("%s%s", &P, &T);
pre();
KMP();
printf("%d\n", ans);
}
return 0;
}