日期:2014-05-17 浏览次数:20422 次
class Trie { protected $dict = array(); protected $buf = ''; function set($word, $value='') { if(is_array($word)) foreach($word as $k=>$v) $this->set($k, $v); $p =& $this->dict; foreach(str_split($word) as $ch) { if(! isset($p[$ch])) $p[$ch] = array(); $p =& $p[$ch]; } $p['val'] = $value; return $this; } function parse($str) { $this->doc = $str; $this->len = strlen($str); $i = 0; while($i < $this->len) { $t = $this->find($this->dict, $i); if($t) { $i = $t; $this->buf = ''; }else $this->buf .= $this->doc{$i++}; } } protected function find(&$p, $i) { if($i >= $this->len) return $i; $t = 0; $n = $this->doc{$i}; if( isset($p[$n]) ) $t = $this->find($p[$n], $i+1); if($t) return $t; if( isset($p['val']) ) { $ar = explode(',', $p['val']); call_user_func_array( array($this, array_shift($ar)), $ar ); return $i; } return $t; } function __call($method, $param) { echo "****\n$this->buf 未定义方法:$method 参数:" . join(',', $param) . "<br />\n"; } }
------解决方案--------------------