KMP算法

在文章里只给出了算法代码以及解释,后边的留下了一份中文一份英文的参考博文地址以便深刻理解KMP算法。ps:中文的亲测,解释原理简单易懂。

KMP算法

算法思想

相比蛮力算法,KMP算法预先计算出了一个哈希表,用来指导在匹配过程中匹配失败后尝试下次匹配的起始位置,以此避免重复的读入和匹配过程。这个哈希表被叫做“部分匹配值表(**Particial match table**)”,它的设计是算法精妙之处。

部分匹配值表

要理解部分匹配值表,就得先了解字符串的前缀(prefix)和后缀(postfix)。

前缀:除字符串最后一个字符以外的所有头部串的组合。
后缀:除字符串第一个字符以外的所有尾部串的组合。
部分匹配值:一个字符串的前缀和后缀中最长共有元素的长度。
举例说明:字符串ABCAB

前缀:{A, AB, ABC, ABCA}
后缀:{BCAB, CAB, AB, B}
部分匹配值:2 (AB)
而所谓的部分匹配值表,则为模式串的所有前缀以及其本身的部分匹配值。
还是针对字符串ABCAB,它的部分匹配值表为:
A B C A B
0 0 0 1 2

算法代码

public static int[] next;

    public static boolean kmp(String str, String dest) {
         for (int i = 0, j = 0; i < str.length(); i ++) {
               while (j > 0 && str.charAt(i) != dest.charAt(j))//iterate to find out the right next position
                    j = next[j - 1];

               if (str.charAt(i) == dest.charAt(j))
                    j ++;

               if (j == dest.length())
                    return true;
          }
          return false;
     }

     public static int[] kmpNext(String str) {
          int[] next = new int[str.length()];
          next[0]  = 0;
          for (int i = 1, j = 0; i < str.length(); i ++) {//j == 0 means the cursor points to nothing.
               //the j here stands for the number of same characters for postfix and prefix, instead of
               //the index of the end of prefix.
                while (j > 0 && strt.charAt(j) != sr.charAt(i))
                    j = next[j - 1]; //watch out here! it's j - 1 here, instead of j

               if (str.charAt(i) == str.charAt(j))
                    j ++;

               next[i] = j;
          }
          return next;
     }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值