-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrietree1.cpp
More file actions
83 lines (79 loc) · 1.69 KB
/
Copy pathtrietree1.cpp
File metadata and controls
83 lines (79 loc) · 1.69 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <string>
struct trienode
{
int path;
int end;
trienode* nexts[26];
trienode(){
path = 0;
end = 0;
for(int i=0;i<26;i++)
{
nexts[i] == NULL;
}
}
};
trienode* root = new trienode();
void insert(string& word)
{
trienode* node = root;
node->path++;
for(int i=0;i<word.length();i++)
{
int path = word[i]-'a';
if(node->nexts[path] == NULL)
node->nexts[path] = new trienode();
node = ndoe->nexts[path];
node->path++;
}
node->end++;
}
void erase(string& word)
{
trienode* node = root;
if(countword(word) > 0)
{
for(int i = 0;i<word.length();i++)
{
int path = word[i]-'a';
trienode* next = node->nexts[path];
if(--next->path == 0)
{
delete next;
node->nexts[path] = NULL;
return;
}
node = next;
node->path--;
}
node->end--;
}
}
int countword(string& word)
{
trienode* node = root;
for(int i=0;i<word.length();i++)
{
int path = word[i]-'a';
if(node->nexts[path] == NULL)
return 0;
node = node->nexts[path];
}
return node->end;
}
int countwordstringwith(string& word)
{
trienode* node = root;
for(int i=0;i<word.length();i++)
{
int path = word[i]-'a';
if(node->nexts[path] == NULL)
return 0;
node = node->nexts[path];
}
return node->path;
}