zoukankan      html  css  js  c++  java
  • String 匹配算法(2)第32章 分类: 算法导论 20110303 10:55 163人阅读 评论(0) 收藏

    3.KMP算法(Knuth-Morris-Pratt )

    该算法的与Rabin-Karp一脉相承。也就是先排除可能不是的,在可能中逐一比较。

    I.π函数的得到:

    COMPUTE-PREFIX-FUNCTION(P)
     1 m ← length[P]
     2 π[1] ← 0
     3 k ← 0
     4 for q ← 2 to m
     5      do while k > 0 and P[k + 1] ≠ P[q]
     6             do k ← π[k]
     7         if P[k + 1] = P[q]
     8            then k ← k + 1
     9         π[q] ← k
    10 return π
    
    II.伪代码算法:
    KMP-MATCHER(T, P) 1 n ← length[T] 2 m ← length[P] 3 π ← COMPUTE-PREFIX-FUNCTION(P) 4 q ← 0 ▹Number of characters matched. 5 for i ← 1 to n ▹Scan the text from left to right. 6 do while q > 0 and P[q + 1] ≠ T[i] 7 do q ← π[q] ▹Next character does not match. 8 if P[q + 1] = T[i] 9 then q ← q + 1 ▹Next character matches. 10 if q = m ▹Is all of P matched? 11 then print "Pattern occurs with shift" i - m 12 q ← π[q] ▹Look for the next match.
    C语言算法实现:

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    pamamiko的学习笔记
    pamamiko的安装
    python基本语法1.1--十进制与十六进制数之间的相互转换
    mnist深入--用卷积神经网络改善mnist
    minst数据集怎么跑
    七大常见排序算法总结(Java语言)
    ubuntu 16.03 Anaconda Tensorflow(CPU)安装
    IntelliJ Idea 2017 免费激活方法
    Ubuntu下安装deb格式的软件
    win10下配置java环境
  • 原文地址:https://www.cnblogs.com/deman/p/4716595.html
Copyright © 2011-2022 走看看