-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatriciaTrie.cpp
More file actions
executable file
·71 lines (69 loc) · 1.52 KB
/
Copy pathPatriciaTrie.cpp
File metadata and controls
executable file
·71 lines (69 loc) · 1.52 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
class PatriciaTrie {
public:
bool end=0;
map<string, unique_ptr<PatriciaTrie>> next;
PatriciaTrie(bool end__=false):end(end__){}
void insert(const string &s, const size_t i)
{
if(i==s.size())
{
end=true;
return;
}
auto l=next.lower_bound(string(1, s[i]));
auto&& ls=l->first;
if(l==next.end() || ls[0]!=s[i])
{
next[s.substr(i)]=make_unique<PatriciaTrie>(true);
return;
}
for(int j=0; j<ls.size(); j++)
{
if(i+j<s.size() && s[i+j]==ls[j])
continue;
//ノード分割
string pref=ls.substr(0,j);
string ss=s.substr(i+j);
string lss=ls.substr(j);
bool ssemp=ss=="";
next[pref]=make_unique<PatriciaTrie>(ssemp);
if(!ssemp)
next[pref]->next[ss]=make_unique<PatriciaTrie>(true);
next[pref]->next[lss]=move(l->second);
next.erase(l->first);
return;
}
l->second->insert(s, i+ls.size());
}
bool find(const string &s, const size_t i) const
{
if(i==s.size())
return end;
auto l=next.lower_bound(string(1, s[i]));
auto&& ls=l->first;
string ssub=s.substr(i);
if(l==next.end() || ls[0]!=s[i])
return false;
for(int j=0;j<ls.size();j++)
if(i+j>=s.size() || ls[j]!=s[i+j]) return false;
return l->second->find(s, i+ls.size());
}
void debug_print(int ls) const
{
string space(ls, ' ');
bool f=0;
if(end)
{
cout<<"\\";
f=1;
}
for(auto&& e:next)
{
if(f)
cout<<endl<<space;
cout<<e.first<<" ";
e.second->debug_print(ls + e.first.size()+1);
f=1;
}
}
};