trie 的应用

xuzuning CSDN会员 xuzuning 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
应 CSDN 要求,收到 CSDN 月饼的应发散分贴
前贴已发,由于僧多粥少。故此结贴,比继续发帖散分

class TTrie {
protected $buffer = array();
protected $dict = array( array() );
protected $input = 0; //字符串当前偏移
protected $backtracking = 0; //字符串回溯位置
public $debug = 0;
public $savematch = 1;

function set($word, $action='') {
if(is_array($word)) {
foreach($word as $k=>$v) $this->set($k, $v);
return;
}
$p = count($this->dict);
$cur = 0; //当前节点号
foreach(str_split($word) as $c) {
if (isset($this->dict[$cur][$c])) { //已存在就下移
$cur = $this->dict[$cur][$c];
continue;
}
$this->dict[$p]= array(); //创建新节点
$this->dict[$cur][$c] = $p; //在父节点记录子节点号
$cur = $p; //把当前节点设为新插入的
$p++;
}
$this->dict[$cur]['acc'] = $action; //一个词结束,标记叶子节点
}

function match($s) {
$ret = array();
$cur = 0; //当前节点,初始为根节点
$i =& $this->input; //字符串当前偏移
$p =& $this->backtracking; //字符串回溯位置
$s .= "\0"; //附加结束符
$len = strlen($s);
$buf = '';
while($i < $len) {
$c = $s{$i};
if(isset($this->dict[$cur][$c])) { //如果存在
$cur = $this->dict[$cur][$c]; //转到对应的位置
if(isset($this->dict[$cur][$s[$i+1]])) {//检查下一个字符是否也能匹配,长度优先
$i++;
continue;
}
if(isset($this->dict[$cur]['acc'])) { //是叶子节点,单词匹配!
if($buf != '') {
$this->buffer[] = $buf;
$buf = '';
}
if($this->savematch) $this->buffer[] = substr($s, $p, $i - $p + 1); //取出匹配位置和匹配的词

$ar = explode(',', $this->dict[$cur]['acc']);
call_user_func_array( array($this, array_shift($ar)), $ar );

$p = $i + 1; //设置下一个回溯位置
$cur = 0; //重置当前节点为根节点
}
} else { //不匹配
$buf .= $s{$p}; //substr($s, $p, $i - $p + 1); //保存未匹配位置和未匹配的内容
$cur = 0; //重置当前节点为根节点
$i = $p; //把当前偏移设为回溯位置
$p = $i + 1; //设置下一个回溯位置
}
$i++; //下一个字符
}
if(trim($buf, "\0")) $this->buffer[] = trim($buf, "\0");
return $this->buffer;
}

function __call($method, $param) {
if($this->debug) printf("偏移:%d 回溯:%d\n", $this->input, $this->backtracking);

}
}
...全文

相关推荐

评论 62
收藏 已收藏
分享
返回顶部
xuzuning CSDN会员
基础编程 21,889 成员 140,329 内容