# KMP字符串匹配算法

CSDN视频网址：http://edu.csdn.net/lecturer/144

KMP算法是一种改进的字符串匹配算法，由D.E.Knuth，J.H.Morris和V.R.Pratt同时发现，因此人们称它为克努特——莫里斯——普拉特操作（简称KMP算法）。KMP算法的关键是利用匹配失败后的信息，尽量减少模式串与主串的匹配次数以达到快速匹配的目的。具体实现就是实现一个next()函数，函数本身包含了模式串的局部匹配信息。时间复杂度O(m+n)。网上很多关于KMP算法的解释，在这里就不多说了，直接通过案例给读者分享，通过代码的分析可以了解其算法的运行原理。

  txt[] =  "THIS IS A TEST TEXT"
pat[] = "TEST"


Pattern found at index 10


  txt[] =  "AABAACAADAABAAABAA"
pat[] = "AABA"


   Pattern found at index 0
Pattern found at index 9
Pattern found at index 13


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void computeLPSArray(char *pat, int M, int *lps);

void KMPSearch(char *pat, char *txt)
{
int M = strlen(pat);
int N = strlen(txt);

// create lps[] that will hold the longest prefix suffix
// values for pattern
int *lps = (int *)malloc(sizeof(int)*M);
int j  = 0;  // index for pat[]

// Preprocess the pattern (calculate lps[] array)
computeLPSArray(pat, M, lps);

int i = 0;  // index for txt[]
while (i < N)
{
if (pat[j] == txt[i])
{
j++;
i++;
}

if (j == M)
{
printf("Found pattern at index %d \n", i-j);
j = lps[j-1];
}

// mismatch after j matches
else if (i < N && pat[j] != txt[i])
{
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j-1];
else
i = i+1;
}
}
free(lps); // to avoid memory leak
}

void computeLPSArray(char *pat, int M, int *lps)
{
int len = 0;  // length of the previous longest prefix suffix
int i;

lps[0] = 0; // lps[0] is always 0
i = 1;

// the loop calculates lps[i] for i = 1 to M-1
while (i < M)
{
if (pat[i] == pat[len])
{
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
if (len != 0)
{
// This is tricky. Consider the example
// AAACAAAA and i = 7.
len = lps[len-1];

// Also, note that we do not increment i here
}
else // if (len == 0)
{
lps[i] = 0;
i++;
}
}
}
}

// Driver program to test above function
int main()
{
char *txt = "ABABDABACDABABCABAB";
char *pat = "ABABCABAB";
KMPSearch(pat, txt);
return 0;
}

# Python program for KMP Algorithm
def KMPSearch(pat, txt):
M = len(pat)
N = len(txt)

# create lps[] that will hold the longest prefix suffix
# values for pattern
lps = [0]*M
j = 0 # index for pat[]

# Preprocess the pattern (calculate lps[] array)
computeLPSArray(pat, M, lps)

i = 0 # index for txt[]
while i < N:
if pat[j] == txt[i]:
i += 1
j += 1

if j == M:
print "Found pattern at index " + str(i-j)
j = lps[j-1]

# mismatch after j matches
elif i < N and pat[j] != txt[i]:
# Do not match lps[0..lps[j-1]] characters,
# they will match anyway
if j != 0:
j = lps[j-1]
else:
i += 1

def computeLPSArray(pat, M, lps):
len = 0 # length of the previous longest prefix suffix

lps[0] # lps[0] is always 0
i = 1

# the loop calculates lps[i] for i = 1 to M-1
while i < M:
if pat[i]==pat[len]:
len += 1
lps[i] = len
i += 1
else:
# This is tricky. Consider the example.
# AAACAAAA and i = 7. The idea is similar
# to search step.
if len != 0:
len = lps[len-1]

# Also, note that we do not increment i here
else:
lps[i] = 0
i += 1

txt = "ABABDABACDABABCABAB"
pat = "ABABCABAB"
KMPSearch(pat, txt)

class KMP_String_Matching
{
void KMPSearch(String pat, String txt)
{
int M = pat.length();
int N = txt.length();

// create lps[] that will hold the longest
// prefix suffix values for pattern
int lps[] = new int[M];
int j = 0;  // index for pat[]

// Preprocess the pattern (calculate lps[]
// array)
computeLPSArray(pat,M,lps);

int i = 0;  // index for txt[]
while (i < N)
{
if (pat.charAt(j) == txt.charAt(i))
{
j++;
i++;
}
if (j == M)
{
System.out.println("Found pattern "+
"at index " + (i-j));
j = lps[j-1];
}

// mismatch after j matches
else if (i < N && pat.charAt(j) != txt.charAt(i))
{
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j-1];
else
i = i+1;
}
}
}

void computeLPSArray(String pat, int M, int lps[])
{
// length of the previous longest prefix suffix
int len = 0;
int i = 1;
lps[0] = 0;  // lps[0] is always 0

// the loop calculates lps[i] for i = 1 to M-1
while (i < M)
{
if (pat.charAt(i) == pat.charAt(len))
{
len++;
lps[i] = len;
i++;
}
else  // (pat[i] != pat[len])
{
// This is tricky. Consider the example.
// AAACAAAA and i = 7. The idea is similar
// to search step.
if (len != 0)
{
len = lps[len-1];

// Also, note that we do not increment
// i here
}
else  // if (len == 0)
{
lps[i] = len;
i++;
}
}
}
}

// Driver program to test above function
public static void main(String args[])
{
String txt = "ABABDABACDABABCABAB";
String pat = "ABABCABAB";
new KMP_String_Matching().KMPSearch(pat,txt);
}
}

• 本文已收录于以下专栏：

## 字符串匹配算法之KMP算法

• 2013-10-16 22:33
• 2.97MB
• 下载

## KMP字符串匹配算法

1、字符串匹配是计算机的基本任务之一。 2、举例来说，有一个字符串”BBC ABCDAB ABCDABCDABDE”，我想知道，里面是否包含另一个字符串”ABCDABD”？ 3、许多算法可以完成这个任...

## 使用C# 优化KMP字符串匹配算法

• daowzq
• 2011-10-14 00:48
• 1992

## C语言KMP字符串匹配算法

Kmp算法是用于求一长字符串和短字符串按一定规律匹配的情况，可以求是否能在长串中找到短串     其思想就是通过一个next数组对短串进行处理，找到短串前后某两个位置是否具有相同的前后缀，用next[...

## 字符串匹配算法KMP Java实现

kmp算法的核心思想：先对搜索字串生成偏移对照表，匹配时从左向右依次比较（bm从右向左，号称比kmp更快），相等则文档和搜索字串的下标+1迭代，否则查表，定位最优的偏移位置（文档下标不变，搜索字串下标...

## KMP字符串匹配算法解析

kmp算法为非常经典的字符串匹配算法。下面先讲解下最原始的字符串匹配方法，也就是暴力求解。时间复杂度为O（m*n） 下面举个例子讲解下暴力大法 如  12314123123b   match为12...

## sunday、kmp、 bm、 horspool字符串匹配算法 code

kmp bm字符串匹配算法代码实现 #include #include #include // 失配时，pat的下一跳位置(pat = "abab"; value[] = {-1, 0, -1...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)