zoukankan      html  css  js  c++  java
  • PHP实现KMP算法

      KMP算法是一种比较高效的字符串匹配算法,关于其讲解,可参考文章 字符串匹配的KMP算法,本文只给出具体的PHP代码实现。

    /**
     * @desc构建next数组
     * @param string $str 模式字符串
     * @return array
     */
    function makeNext($str) {
        $len = strlen($str);
        
        $next = [0];
        for($pos=1, $k=$next[0]; $pos<$len; $pos++) {
            if($str[$k] == $str[$pos]) {
                $k++;
            } else {
                while($k>0 && $str[$pos]!=$str[$k]) {
                    $k = $next[$k-1];
                }
            }
            
            $next[$pos] = $k;
        }
    
        return $next;
    }
    
    /**
     * @param string $tString  目标字符串
     * @param string $pString  模式字符串
     * @param bool|false $findAll 是否找出模式串出现的全部位置
     */
    function kmp($tString, $pString, $findAll=false) {
        $lenT = strlen($tString);
        $lenP = strlen($pString);  
        $next = makeNext($pString);
        $found = false;
      
        for ($pos=0, $k=0; $pos<$lenT; $pos++) {
            if ($pString[$k] == $tString[$pos]) {
                $k++;
            } else {
                while($k>0 && $pString[$k] != $tString[$pos]) {
                    $k = $next[$k-1];
                }
            }  
            if ($k == $lenP) {
                $found = true;
                echo 'pattern found at '.($pos-$lenP+1) . PHP_EOL;
                if($findAll) {
                    //匹配后需要退回到当前模式串出现的位置 下次循环从下一位置重新开始匹配
                    $pos = $pos-$lenP+1;
                    $k = 0;
                } else {
                    break;                
                }
            } 
        }
        if(! $found) {
            echo 'pattern not found';
        }
    }
    
    kmp('abacabcaba', 'aba', 1);
  • 相关阅读:
    codeAnalyze_函数赋值给另一个函数的形参
    js_new关键字创建对象的五个步骤
    codeRecord_bind
    js_活动对象与变量对象的区别
    将linux的随机ip固定为设置的固定ip
    Springcloud总结
    Jackson的使用
    Lucene的初步了解和学习
    Shiro安全框架
    关于xpath中的tbody
  • 原文地址:https://www.cnblogs.com/cnsr/p/8184139.html
Copyright © 2011-2022 走看看