forked from arshdeep/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.cpp
More file actions
149 lines (133 loc) · 2.74 KB
/
Trie.cpp
File metadata and controls
149 lines (133 loc) · 2.74 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
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <sstream>
#include <set>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <time.h>
using namespace std;
struct Trie
{
int words;
int prefixes;
Trie *edges[26];
};
class Dict
{
private:
Trie *trie;
void cutLeftmostCharacter(string &str)
{
str.erase(str.begin());
}
public:
void initialize(Trie *vertex);
void addWord(Trie *vertex, string word);
int countPrefixes(Trie *vertex, string prefix);
int countWords(Trie *vertex, string word);
int countWords(Trie *vertex, string word, int missingLetters);
};
void Dict::initialize(Trie *vertex)
{
vertex->words = 0;
vertex->prefixes = 0;
for (int i = 0; i < 26; ++i)
{
vertex->edges[i] = NULL;
}
}
void Dict::addWord(Trie *vertex, string word)
{
if (word.length() == 0)
vertex->words = vertex->words + 1;
else
{
vertex->prefixes = vertex->prefixes + 1;
int k = word[0] - 'a';
if(vertex->edges[k] == NULL)
{
vertex->edges[k] = new Trie();
initialize(vertex->edges[k]);
}
cutLeftmostCharacter(word);
addWord(vertex->edges[k], word);
}
}
int Dict::countWords(Trie *vertex, string word)
{
if (word.length() == 0)
return vertex->words;
else
{
int k = word[0] - 'a';
if (vertex->edges[k] == NULL)
return 0;
else
{
cutLeftmostCharacter(word);
return countWords(vertex->edges[k], word);
}
}
}
int Dict::countPrefixes(Trie *vertex, string prefix)
{
if (prefix.length() == 0)
return vertex->prefixes;
else
{
int k = prefix[0] - 'a';
if (vertex->edges[k] == NULL)
return 0;
else
{
cutLeftmostCharacter(prefix);
return countPrefixes(vertex->edges[k], prefix);
}
}
}
int Dict::countWords(Trie *vertex, string word, int missingLetters)
{
if (word.length() == 0)
return vertex->words;
else
{
int k = word[0] - 'a';
if (vertex->edges[k] == NULL && missingLetters == 0)
{
return 0;
}
else if (vertex->edges[k] == NULL)
{
//Here we cut a character but we don't go lower in the tree
cutLeftmostCharacter(word);
return countWords(vertex, word, missingLetters - 1);
}
else
{
//We are adding the two possibilities: the first
//character has been deleted plus the first character is present
int r = countWords(vertex, word, missingLetters - 1);
cutLeftmostCharacter(word);
r = r + countWords(vertex->edges[k], word, missingLetters);
return r;
}
}
}
int main()
{
Dict dict;
Trie trie;
dict.initialize(&trie);
dict.addWord(&trie, "tree");
dict.addWord(&trie, "trie");
dict.addWord(&trie, "algo");
dict.addWord(&trie, "assoc");
dict.addWord(&trie, "all");
dict.addWord(&trie, "also");
cout<<dict.countWords(&trie, "tee");
getchar();
return 0;
}