-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.php
More file actions
138 lines (124 loc) · 2.97 KB
/
trie.php
File metadata and controls
138 lines (124 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/*
wangxx created this file on Feb 27,2017
unix2dos SpecialNum.txt
unix2dos NumberSeg.txt
*/
error_reporting (E_ALL & ~E_NOTICE);
class Trie {
private $trie;
function __construct( ){
$trie=array('children'=>array( ), 'isword'=>false);
}
function &setWord($word=''){
$trienode=&$this->trie;
for($i=0;$i<strlen($word);$i++){
$character=$word[$i];
if(!isset($trienode['children'][$character])){
$trienode['children'][$character]=array('isword'=>false);
}
if($i==strlen($word)-1){
$trienode['children'][$character]=array('isword'=>true);
}
// print_r($trienode);
$trienode=& $trienode['children'][$character];
}
// print_r($this->trie);
}
function &isWord($word){
$trienode=&$this->trie;
for($i=0;$i<strlen($word);$i++){
$character=$word[$i];
if(!isset($trienode['children'][$character])){
return false;
}else{
if($i==(strlen($word)-1)&&$trienode['children'][$character]['isword']==true){
return true;
}
elseif($i==(strlen($word)-1)&&$trienode['children'][$character]['isword']==false){
return false;
}
$trienode=&$trienode['children'][$character];
}
}
}
function search($text=""){
$textlen=strlen($text);
$trienode=$tree=$this->trie;
$find=array();
$wordrootposition=0;
$prenode=false;
$word='';
for($i=0;$i<$textlen;$i++){
if(isset($trienode['children'][$text[$i]])){
$word=$word.$text[$i];
$trienode=$trienode['children'][$text[$i]];
if ($prenode==false){
$wordrootposition=$i;
}
$prenode=true;
if($trienode['isword']){
$find[]=array('position'=>$wordrootposition,'word'=>$word);
}
}else{
$trienode=$tree;
$word='';
if($prenode){
$i=$i-1;
$prenode=false;
}
}
}
// print_r($find);
return $find;
}
function searchMax($text=""){
$textlen=strlen($text);
$trienode=$tree=$this->trie;
$find=array();
$wordrootposition=0;
$prenode=false;
$word='';
for($i=0;$i<$textlen;$i++){
if(isset($trienode['children'][$text[$i]])){
$word=$word.$text[$i];
$trienode=$trienode['children'][$text[$i]];
if ($prenode==false){
$wordrootposition=$i;
}
$prenode=true;
if($trienode['isword']){
$find[]=array('position'=>$wordrootposition,'word'=>$word);
}
}else{
$trienode=$tree;
$word='';
if($prenode){
$i=$i-1;
$prenode=false;
}
}
}
// print_r($find);
$n=count($find)-1;
return $find[$n];
}
}
$trie=new Trie();
$trie->setWord('156');
$trie->setWord('15618212');
$trie->setWord('82397');
$trie->setWord('7283482');
$trie->setWord('345435');
$trie->setWord('5656');
$trie->setWord('2343');
$trie->setWord('45654');
print_r($trie);
$maxWord=$trie->searchMax('15618212766');
print_r($maxWord);
echo 'maxWord is '.$maxWord['word']."\n";
// $words=$trie->searchMax('abcdefgh');
// foreach($words as $word){
// echo 'position:'.$word['position'].'-'.(strlen($word['word'])+$word['position']."\n");
// echo 'word---'.$word['word']."\n";
// }