[总结]KMP算法

refer:

1. 原理
字符串匹配是计算机的基本任务之一。
举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD"?
许多算法可以完成这个任务,Knuth-Morris-Pratt算法(简称KMP)是最常用的之一。它以三个发明者命名,起头的那个K就是著名科学家Donald Knuth
这种算法不太容易理解,网上有很多解释,但读起来都很费劲。直到读到Jake Boxer的文章,我才真正理解这种算法。下面,我用自己的语言,试图写一篇比较好懂的KMP算法解释。
[总结]KMP算法
首先,字符串"BBC ABCDAB ABCDABCDABDE"的第一个字符与搜索词"ABCDABD"的第一个字符,进行比较。因为B与A不匹配,所以搜索词后移一位。
[总结]KMP算法

因为B与A不匹配,搜索词再往后移。
[总结]KMP算法

就这样,直到字符串有一个字符,与搜索词的第一个字符相同为止。
[总结]KMP算法
接着比较字符串和搜索词的下一个字符,还是相同。
[总结]KMP算法
直到字符串有一个字符,与搜索词对应的字符不相同为止。
[总结]KMP算法
这时,最自然的反应是,将搜索词整个后移一位,再从头逐个比较。这样做虽然可行,但是效率很差,因为你要把"搜索位置"移到已经比较过的位置,重比一遍。
[总结]KMP算法
一个基本事实是,当空格与D不匹配时,你其实知道前面六个字符是"ABCDAB"。KMP算法的想法是,设法利用这个已知信息,不要把"搜索位置"移回已经比较过的位置,继续把它向后移,这样就提高了效率。
[总结]KMP算法
怎么做到这一点呢?可以针对搜索词,算出一张《部分匹配表》(Partial Match Table)。这张表是如何产生的,后面再介绍,这里只要会用就可以了。

[总结]KMP算法
已知空格与D不匹配时,前面六个字符"ABCDAB"是匹配的。查表可知,最后一个匹配字符B对应的"部分匹配值"为2,因此按照下面的公式算出向后移动的位数:
  移动位数 = 已匹配的字符数 - 对应的部分匹配值
因为 6 - 2 等于4,所以将搜索词向后移动4位。
[总结]KMP算法
因为空格与C不匹配,搜索词还要继续往后移。这时,已匹配的字符数为2("AB"),对应的"部分匹配值"为0。所以,移动位数 = 2 - 0,结果为 2,于是将搜索词向后移2位。
[总结]KMP算法
因为空格与A不匹配,继续后移一位。
[总结]KMP算法
逐位比较,直到发现C与D不匹配。于是,移动位数 = 6 - 2,继续将搜索词向后移动4位。
[总结]KMP算法
逐位比较,直到搜索词的最后一位,发现完全匹配,于是搜索完成。如果还要继续搜索(即找出全部匹配),移动位数 = 7 - 0,再将搜索词向后移动7位,这里就不再重复了。
下面介绍《部分匹配表》是如何产生的。
首先,要了解两个概念:"前缀"和"后缀"。 "前缀"指除了最后一个字符以外,一个字符串的全部头部组合;"后缀"指除了第一个字符以外,一个字符串的全部尾部组合。
[总结]KMP算法
"部分匹配值"就是"前缀"和"后缀"的最长的共有元素的长度。以"ABCDABD"为例,
  - "A"的前缀和后缀都为空集,共有元素的长度为0;
  - "AB"的前缀为[A],后缀为[B],共有元素的长度为0;
  - "ABC"的前缀为[A, AB],后缀为[BC, C],共有元素的长度0;
  - "ABCD"的前缀为[A, AB, ABC],后缀为[BCD, CD, D],共有元素的长度为0;
  - "ABCDA"的前缀为[A, AB, ABC, ABCD],后缀为[BCDA, CDA, DA, A],共有元素为"A",长度为1;
  - "ABCDAB"的前缀为[A, AB, ABC, ABCD, ABCDA],后缀为[BCDAB, CDAB, DAB, AB, B],共有元素为"AB",长度为2;
  - "ABCDABD"的前缀为[A, AB, ABC, ABCD, ABCDA, ABCDAB],后缀为[BCDABD, CDABD, DABD, ABD, BD, D],共有元素的长度为0。
[总结]KMP算法
"部分匹配"的实质是,有时候,字符串头部和尾部会有重复。比如,"ABCDAB"之中有两个"AB",那么它的"部分匹配值"就是2("AB"的长度)。搜索词移动的时候,第一个"AB"向后移动4位(字符串长度-部分匹配值),就可以来到第二个"AB"的位置。

2. C++源码
<span style="color:#464646;">#include "stdafx.h"
#include
using namespace std;

void get_next(char*t, int next[ ]){
 int t_len=strlen(t);
 int i=0;         //求解每个next[i]
 next[0]=-1; //递推基本条件,然后求解next[i+1]
 int j=-1;     //向后递推位置下标
 </span><ol start="1" class="dp-cpp" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 0px 0px 1px 45px !important;"><li style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgb(153, 153, 153); border-image: initial; list-style-type: decimal-leading-zero; list-style-image: initial; background-color: rgb(249, 249, 249); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; list-style-position: outside !important;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> <span class="comment" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">/*</span> </span></li><li class="alt" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgb(153, 153, 153); border-image: initial; list-style-type: decimal-leading-zero; list-style-image: initial; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; list-style-position: outside !important;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> next[i]=k =>T0...Tk-1=Ti-k...Ti-1</span> </span></li><li style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgb(153, 153, 153); border-image: initial; list-style-type: decimal-leading-zero; list-style-image: initial; background-color: rgb(249, 249, 249); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; list-style-position: outside !important;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">    求解next[i+1]</span> </span></li><li class="alt" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgb(153, 153, 153); border-image: initial; list-style-type: decimal-leading-zero; list-style-image: initial; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; list-style-position: outside !important;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> 1> 如果T0..Tk-1Tk=Ti-k...Ti-1Ti=>next[i+1]=k+1=next[i]+1;</span> </span></li><li style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgb(153, 153, 153); border-image: initial; list-style-type: decimal-leading-zero; list-style-image: initial; background-color: rgb(249, 249, 249); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; list-style-position: outside !important;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> 2>Tk<>Ti,next[k]=k', 如果Ti=Tk'=>next[i+1]=k'+1=next[k]+1=next[next[i]]+1;</span> </span></li><li class="alt" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgb(153, 153, 153); border-image: initial; list-style-type: decimal-leading-zero; list-style-image: initial; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; list-style-position: outside !important;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> 3>依次递推 最后情况next[i+1]=next[0]+1=0,即</span> </span></li><li style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgb(153, 153, 153); border-image: initial; list-style-type: decimal-leading-zero; list-style-image: initial; background-color: rgb(249, 249, 249); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; list-style-position: outside !important;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> */</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li></ol><span style="color:#464646;">
 while(i<t_len)
 {
   if(j==-1 ||t[i]==t[j])  //j==-1证明已经与t[0]不匹配了,此时next[i+1]=0
   {
    i++;
    j++;
    next[i]=j;
   }
   else
   {
       j=next[j]; 
   }
 }
}
int KMP(char *s,char *t){
 int s_len=strlen(s);
 int t_len=strlen(t);
 int i=0;
 int j=0;
 int *next=new int[t_len];
 get_next(t,next);
 if(t_len>s_len) return -1;
 <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: green; font-weight: bold; font-family: Consolas, "Courier New", Courier, mono, serif;">while</span><span style="margin: 0px; padding: 0px; border: none; font-family: Consolas, "Courier New", Courier, mono, serif;">(i<s_len&&j<t_len){  </span>
  if(j==-1||s[i]==t[j]){
   i++;
   j++;
  }
  else{
  j=next[j];
  }
 }//end while
 if(j>=t_len)
  return i-j;
 else
  return -1;
}
int main(void)
 {
  char *s="abcdasdefghijklmnefgh";
  char *t="efgh";
  cout<<KMP(s,t)<<endl;
       return 0;
 }</span>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值