BMH子串查找算法(PHP实现)

简介:
代码
interface StringSearchable
{
    public function search($substring, $buffer);
}
class BoyerMooreStringSearch implements StringSearchable
{
    public $substring = null;
    public $buffer = '';
    public $jumpTable = array();
    protected $results = array();

    public function __construct()
    {    
        
    }
    public function __destruct()
    {
    }
    public function search($substring, $buffer)
    {    
        $this->results = array();
        $this->substring = $substring;
        $this->buffer = $buffer;
        $this->deriveJumpTable();
        
        $substringLen = strlen($this->substring);
        $currentCharIndex = $substringLen - 1;
        $bufferLen = strlen($this->buffer);
        while ($currentCharIndex < $bufferLen) {    
            for ($i = $substringLen - 1; $i >= 0; $i--) {
                if ($this->buffer[$currentCharIndex - $substringLen + $i + 1] == $this->substring[$i]) {
                    if ($i == 0) {
                        $this->results[] = $currentCharIndex - $substringLen;
                        $currentCharIndex += $this->getJumpLength($this->buffer[$currentCharIndex]);
                    } else {
                        continue;
                    }
                } else {
                    $currentCharIndex += $this->getJumpLength($this->buffer[$currentCharIndex]);
                    break;
                }

            }
        }
        return (sizeof($this->results) > 0);
    }
    
    protected function deriveJumpTable()
    {
        $maxJump = strlen($this->substring);
        for ($i = strlen($this->substring) - 2; $i >= 0; $i--) {
            if (!array_key_exists($this->substring[$i], $this->jumpTable)) {
                $this->jumpTable[$this->substring[$i]]] = $maxJump - $i -1;
            }
        }
    }
    public function getJumpTable()
    {
        return $this->jumpTable;    
    }
    public function getResults()
    {
        return $this->results;
    }
    public function getResultsCount()
    {
        return sizeof($this->results);
    }
    public function getJumpLength($charIndex)
    {
        if (array_key_exists($charIndex, $this->jumpTable)) {
            return $this->jumpTable[$charIndex];
        } else {
            return strlen($this->substring);
        }
    }
}

function Main()
{    
    $poem = <<<POEM
    you son of bitch
    god damn it
    hey,god love me
    POEM;
    $bm = new BoyerMooreStringSearch();
    $bm->search('god', $poem);
    $count = $bm->getResultsCount;
    echo $count;
}
复制代码



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2010/04/21/1717668.html,如需转载请自行联系原作者
目录
相关文章
|
PHP
php实现数字格式化,数字每三位加逗号的功能函数169856420=&gt;169,856,420
php实现数字格式化,数字每三位加逗号的功能函数169856420=&gt;169,856,420
188 0
|
PHP
PHP实现Workerman实例 高性能PHP Socket即时通讯框架
PHP实现Workerman实例 高性能PHP Socket即时通讯框架
359 0
|
消息中间件 PHP Windows
PHP实现php-amqplib/php-amqplib实例RabbitMq
PHP实现php-amqplib/php-amqplib实例RabbitMq
123 0
|
XML 移动开发 JSON
PHP使用yansongda/pay实现支付宝和微信的支付
PHP使用yansongda/pay实现支付宝和微信的支付
849 0
|
NoSQL PHP Redis
PHP结合redis实现点赞功能
PHP结合redis实现点赞功能
107 0
|
消息中间件 缓存 JSON
PHP实现think-queue介绍
PHP实现think-queue介绍
329 0
|
PHP
php实现定时任务hellogerard/jobby
php实现定时任务hellogerard/jobby
114 0
|
PHP
PHP实现JWT lcobucci/jwt生成jwt token
PHP实现JWT lcobucci/jwt生成jwt token
429 0
|
NoSQL PHP 调度
PHP实现定时任务hellogerard/jobby实例
PHP实现定时任务hellogerard/jobby实例
111 0
|
PHP
PHP实现极光推送jpush/jpush 手机APP消息推送
PHP实现极光推送jpush/jpush 手机APP消息推送
308 0