本文参考自:https://www.cnblogs.com/ZuoAndFutureGirl/p/9028287.html
假设现在我们面临这样一个问题:有一个文本串S,和一个模式串P,现在要查找P在S中的位置,怎么查找呢?
如果用暴力匹配的思路,并假设现在文本串S匹配到 i 位置,模式串P匹配到 j 位置,则有:
- 如果当前字符匹配成功(即S[i] == P[j]),则i++,j++,继续匹配下一个字符;
- 如果失配(即S[i]! = P[j]),令i = i - (j - 1),j = 0。相当于每次匹配失败时,i 回溯,j 被置为0。
暴力匹配的代码,如下:
int ViolentMatch(char* s, char* p)
{
int sLen = strlen(s);
int pLen = strlen(p);
int i = 0;
int j = 0;
while(i < sLen && j < pLen){
if(s[i] == p[j]){
//①如果当前字符匹配成功(即S[i] == P[j]),则i++,j++
i++;
j++;
}
else{
//②如果失配(即S[i]! = P[j]),令i = i - (j - 1),j = 0
i = i - j + 1;
j = 0;
}
}
//匹配成功,返回模式串p在文本串s中的位置,否则返回-1
if(j==pLen)
return i - j;
else
return -1;
}
举个例子,如果给定文本串S = “BBCABCDABABCDABCDABDE”,和模式串P = “ABCDABD”,现在要拿模式串P去跟文本串S匹配,整个过程如下所示:
- S[0] 为 B,P[0] 为 A,不匹配,执行第2条指令:“如果匹配失败(即S[i]! = P[j]),令i = i - (j - 1),j = 0”,S[1] 跟 P[0]匹配,相当于模式串要往右移动一位(i=1,j=0)
- S[1] 跟 P[0] 还是不匹配,继续执行第2条指令:“如果匹配失败(即S[i]! = P[j]),令i = i - (j - 1),j = 0”,S[2]跟P[0]匹配(i=2,j=0),从而模式串不断的向右移动一位(不断的执行“令i = i - (j - 1),j = 0”,i从2变到4,j一直为0)
3. 直到 S[4] 跟 P[0] 匹配成功(i=4,j=0),此时按照上面的暴力匹配算法的思路,转而执行第1条指令:“如果当前字符匹配成功(即S[i] == P[j]),则i++,j++”,可得S[i]为S[5],P[j]为P[1],即接下来S[5]跟P[1]匹配(i=5,j=1)
4. S[5] 跟 P[1] 匹配成功,继续执行第1条指令:“如果当前字符匹配成功(即S[i] == P[j]),则i++,j++”,得到S[6]跟P[2]匹配(i=6,j=2),如此进行下去
5. 直到S[10]为空格字符,P[6]为字符D(i=10,j=6),因为不匹配,重新执行第2条指令:“如果匹配失败(即S[i]! = P[j]),令i = i - (j - 1),j = 0”,相当于S[5]跟P[0]匹配(i=5,j=0)
6. 至此,我们可以看到,如果按照暴力匹配算法的思路,尽管之前文本串和模式串已经分别匹配到了S[9]、P[5],但因为S[10]跟P[6]不匹配,所以文本串回溯到S[5],模式串回溯到P[0],从而让S[5]跟P[0]匹配。
而S[5]肯定跟P[0]失配。为什么呢?因为在之前第4步匹配中,我们已经得知S[5] = P[1] = B,而P[0] = A,即P[1] != P[0],故S[5]必定不等于P[0],所以回溯过去必然会导致失配。那有没有一种算法,让i 不往回退,只需要移动j 即可呢?
答案是肯定的。这种算法就是本文的主旨KMP算法,它利用之前已经部分匹配这个有效信息,保持 i 不回溯,通过修改 j 的位置,让模式串尽量地移动到有效的位置。
KMP算法
Knuth-Morris-Pratt 字符串查找算法,简称"KMP"算法,常用于在一个文本串 S 内查找模式串 P 的出现位置,时间复杂度:O(m+n)。
流程:
- 假设现在文本串 S 匹配到 i 位置,模式串 P 匹配到 j 位置
- 如果 j = -1,或者当前字符匹配成功(即S[i] == P[i]),令 i++,j++,继续匹配下一个字符;
- 如果 j != -1,且当前字符串匹配失败(即S[i] != P[i]),则令 i 不变,j = nxt[j]。此举意味着匹配失败时,模式串 P 相当于文本串 S 向右移动了 j - nxt[j] 位。
- 换而言之,当匹配失败时,模式串向右移动的次数为:匹配失败字符所在的位置 - 匹配失败字符对应的 nxt 值(nxt 数组的求解会在下面详细阐述),即移动的实际步数:j - nxt[j],且此值大于等于 1。
很快,你会意识到 nxt 数组各值的含义:代表当前字符之前的字符串中,有多大长度的相同前缀后缀 。列如 nxt[j] = k,代表 j 之前的字符串中有最大长度为 k 的相同前缀后缀。
此也意味着在某些字符失配时,该字符对应的 nxt 值会告诉你下次匹配中,模式串应该跳到哪个位置(跳到 next[j] 的位置)。如果 nxt[j] 等于0或者-1,则就跳到模式串的开头字符,若 nxt[j] = k 且 k>0,代表下次匹配跳到 j 之前的某个字符,而不是开头,且具体跳过了 k 个字符。
int kmp(int sl, int pl) //sl,pl分别是字符串s,p的长度
{
memset(nxt,-1,sizeof(nxt));
get_nxt(pl); //获取 nxt 值
int i = 0;
int j = 0;
while(i<sl&&j<pl){
//1.如果 j==-1,或者是当前字符串匹配成功,都令 i++,j++
if(j==-1||s[i]==p[j]){
i++;
j++;
}
else{
//2.如果 j!=-1,且当前字符串匹配失败,则令 i 不变,j = nxt[j]
//nxt[j]即为j所对应的nxt值
j = nxt[j];
}
}
if(j==pl) return i-j;
return -1;
}
nxt数组的获得
void get_nxt(int pl)
{
int i,j;
i = 0;
j = nxt[0] = -1;
while(i<pl){
if(j==-1||p[i]==p[j]){
i++,j++;
nxt[i] = j;
}
else
j = nxt[j];
}
}
优化后:
void get_nxt(int pl)
{
nxt[0] = -1;
int k = -1;
int j = 0;
while(j<pl-1){
//p[k]表示前缀,p[j]表示后缀
if (k==-1||p[j]==p[k]){
++j;
++k;
if(p[j]!=p[k])
nxt[j] = k;
else
nxt[j] = nxt[k];
}
else{
k = nxt[k];
}
}
}
http://acm.hdu.edu.cn/showproblem.php?pid=1711
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
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
#include<map>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int maxn = 1000101;
int nxt[maxn];
int p[maxn],s[maxn];
void get_nxt(int pl)
{
nxt[0] = -1;
int k = -1;
int j = 0;
while(j<pl-1){
if (k==-1||p[j]==p[k]){
++j;
++k;
if(p[j]!=p[k])
nxt[j] = k;
else
nxt[j] = nxt[k];
}
else{
k = nxt[k];
}
}
}
int kmp(int sl, int pl)
{
memset(nxt,-1,sizeof(nxt));
get_nxt(pl);
int i = 0;
int j = 0;
while(i<sl&&j<pl){
if(j==-1||s[i]==p[j]){
i++;
j++;
}
else{
j = nxt[j];
}
}
if(j==pl) return i-j;
return -1;
}
int main(void)
{
int t,m,n;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&s[i]);
for(int i=0;i<m;i++)
scanf("%d",&p[i]);
int ans = kmp(n,m);
printf("%d\n",ans==-1 ? ans : ans+1);
}
return 0;
}
http://acm.hdu.edu.cn/showproblem.php?pid=2087
Problem Description
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
Sample Input
abcde a3
aaaaaa aa
Sample Output
0
3
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
#include<map>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int maxn = 1000101;
int nxt[maxn];
string p,s;
void get_nxt(int pl)
{
nxt[0] = -1;
int k = -1;
int j = 0;
while(j<pl-1){
if (k==-1||p[j]==p[k]){
++j;
++k;
if(p[j]!=p[k])
nxt[j] = k;
else
nxt[j] = nxt[k];
}
else{
k = nxt[k];
}
}
}
int kmp(int sl, int pl)
{
get_nxt(pl);
int cnt = 0;
int i = 0;
int j = 0;
while(i<sl&&j<pl){
if(j==-1||s[i]==p[j]){
i++;
j++;
}
else{
j = nxt[j];
}
if(j==pl){
cnt++;
j = 0;
}
}
return cnt;
}
int main(void)
{
while(cin>>s&&s[0]!='#'){
cin>>p;
memset(nxt,-1,sizeof(nxt));
int pl = p.length();
int sl = s.length();
cout<<kmp(sl,pl)<<'\n';
}
return 0;
}