Chinaunix首页 | 论坛 | 博客
  • 博客访问: 284478
  • 博文数量: 82
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 874
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-21 09:58
个人简介

traveling in cumputer science!!

文章分类

全部博文(82)

文章存档

2016年(13)

2015年(69)

我的朋友

分类: C/C++

2016-03-13 13:44:08

1.KMP算法理解

1.1

此方法是一种常用的提高字符串匹配效率的算法,由作者Knuth–Morris–Pratt得名。但实际意义是字符串匹配过程中,省略不必要操作的一种方法。
最基础的方法是令匹配模板pattern,从待匹配string的头开始,逐位后移进行操作。如下:

基于KMP算法的匹配方法如下:

如上所示,KMP只用了3次基于pattern的匹配方法就完成了对string的匹配操作,就是这么滴有效率,所以很有必要搞明白!!~_~   

1.2

要完成如上的kmp操作,首先我们要获得一个数组,她记录着此次pattern匹配完成或匹配失败该向后移动多少个单位,然后再重新开始匹配。
这个数组的计算就是KMP的核心内容:

首先:

我们要明白,为什么上述的KMP操作的第二次匹配是直接后移了2个单位后进行的。因为这些数据信息我们从pattern本身就能获得,你看pattern是"abab",第一次匹配时到第二个a的时候因匹配失败结束,但从"abab"我们可以知道,下一次匹配我们可以直接从string中对应pattern中第二个a的位置开始。因为能匹配到第二个a说明pattern的a前面的部分全部匹配成功,而a后面是b所以向后移动一个单位进行匹配的操作就没有必要了,因为已知后面是b,而开始元素是a,明明知道不能匹配还去操作,这不是傻吗...

接下来:

我们要总结一下上述这种思想的特点,使她能够实现在我们的程序中,让计算机也知道有这么一回事儿。要知道需要向后移动多少单位,我们首先要知道两个数据,一个是我们目前已经匹配的长度,另一个关键数据就是“基于目前已经匹配的字符串的那个:即是自身真后缀又是自身最长前缀的子串”的长度。这句话有点绕啊,不过算法的关键就在这句上了.....下面针对这句比较绕的定义,举个栗子:
例如,pattern 是 'ABCABDFG'
'ABCABDFG' 绿色的是目前匹配到的位置,那么ABCA的“即是自身真后缀又是自身最长前缀的子串”就是A       需要向后移动的单位h = 4 - 1, 3个
'ABCABDFG' 绿色的是目前匹配到的位置,那么ABCAB的“即是自身真后缀又是自身最长前缀的子串”就是AB  需要向后移动的单位h = 5 - 2 ,3个

最后:

我们就可以把这个对应匹配进度应该向后移动多少单位的记录存储到一个数组中,这可谓是一件好装备,然后应用到我们的匹配团战中,发挥她的威力!!

2.上述数组的计算流程

k:记录上次的“即是自身真后缀又是自身最长前缀的子串”的长度
m:记录当前匹配字符个数
ARR:记录移动单位数据的数组
P:pattern字符串
过程如下:
k = 0;
ARR[0]=1;
m = 2;
curr_char =P[m-1]; //当前个数比数组下标大1,下标从0开始
while(curr_char!='\0')
{
        if(P[k] == curr_char)
            k += 1;
        else
            k = 0;
        ARR[m-1] = m - k;
        m += 1;
        curr_char = P[m-1];
}

3.C++代码实现

kmp.cpp

点击(此处)折叠或打开

  1. /*****************************
  2. filename: kmp.cpp
  3. description: realize the kmp match algorithm
  4. author: warrior mail:15933533880@163.com
  5. date:2016-3-12
  6. log:
  7. *****************************/

  8. /****** include *******/
  9. #include "kmp.h"
  10. using namespace std;
  11. /****** define *********/
  12. /****** variable ********/
  13. /****** function *******/
  14. /****************
  15. description: get the max prefix of any index of pattern
  16. input:
  17.     string str_pattern
  18.     int * prefixRecord
  19. output:
  20.     none
  21. ****************/
  22. void MaxPrefix(char * str_pattern, int * prefixRecord)
  23. {
  24.     int lastPrefixRecord = 0; //k
  25.     int currentIndex = 0; //m
  26.     char curr_char = 'a';
  27.     if((str_pattern==NULL)|(prefixRecord==NULL))
  28.     {
  29.         cout<<"input error!!"<<endl;
  30.         return ;
  31.     }
  32.     lastPrefixRecord = 0;
  33.     prefixRecord[0]=1;
  34.     currentIndex = 2;
  35.     curr_char = str_pattern[currentIndex-1];
  36.     while(curr_char!='\0')
  37.     {
  38.         if(str_pattern[lastPrefixRecord]==curr_char)
  39.             lastPrefixRecord += 1;
  40.         else
  41.             lastPrefixRecord = 0;

  42.         prefixRecord[currentIndex-1] = currentIndex - lastPrefixRecord;
  43.         currentIndex += 1;
  44.         curr_char = str_pattern[currentIndex-1];
  45.     }
  46. }
kmp.h

点击(此处)折叠或打开

  1. #ifndef _KMP_H_
  2. #define _KMP_H_
  3. /****** include *******/
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <string>
  7. /****** define *********/
  8. /****** variable ********/
  9. /****** function *******/
  10. void MaxPrefix(char * str_pattern, int * prefixRecord);

  11. #endif // _KMP_H_
main.cpp

点击(此处)折叠或打开

  1. #include "kmp.h"
  2. #include <string.h>
  3. using namespace std;

  4. int main()
  5. {

  6.     string str_1("abcdabcabcdaef");
  7.     char *str_2 = new char[str_1.length()];
  8.     strcpy(str_2, str_1.c_str());
  9.     int* preARR = new int[str_1.length()];
  10.     MaxPrefix(str_2, preARR);
  11.     for(int i=0 ;i<str_1.length(); i++)
  12.     {
  13.         cout<<" "<<preARR[i];
  14.     }
  15.     cout<<endl;
  16.     delete str_2;
  17.     return 0;
  18. }

4.总结

参考链接:
http://blog.sina.com.cn/s/blog_6cf48afb0100n561.html
%E2%80%93Morris%E2%80%93Pratt_algorithm



.
阅读(1391) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~