-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordTrie.cpp
100 lines (92 loc) · 2.07 KB
/
WordTrie.cpp
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
#include "WordTrie.h"
WordTrie * WordTrie::AddNode(char c, std::string m, std::string r)
{
childNodes.push_back(new WordTrie(c, r, m));
return childNodes[childNodes.size() - 1];
}
std::string WordTrie::FindExpression(std::string string, int currentCharIndex)
{
if (string.size() < 2) return "";
char currentChar = string[currentCharIndex];
for (WordTrie * child : childNodes)
{
if (child->character == currentChar)
{
if (currentCharIndex == string.size()-1)
{
return child->reply;
}
else
{
return child->FindExpression(string, currentCharIndex + 1);
}
}
}
return "";
}
WordTrie * WordTrie::CreateNode(std::string string, int currentCharIndex, std::string r)
{
if (string.size() < 2) throw "Ehh :(";
char currentChar = string[currentCharIndex];
for (WordTrie * child : childNodes)
{
if (child->character == currentChar)
{
return child->CreateNode(string, currentCharIndex + 1, r);
}
}
if (currentCharIndex == string.size() - 1)
{
return AddNode(currentChar, string, r);
}
else return CreateSubtrie(string, currentCharIndex, r);
}
WordTrie * WordTrie::CreateSubtrie(std::string string, int currentCharIndex, std::string r)
{
if (currentCharIndex == string.size() - 1)
{
reply = r;
message = string;
return this;
}
char currentChar = string[currentCharIndex];
return AddNode(currentChar, " ", " ")->CreateSubtrie(string, currentCharIndex + 1, r);
}
std::stringstream WordTrie::AllReplies()
{
std::stringstream out;
for (WordTrie * x : childNodes)
{
out << x->GetReplies();
}
return out;
}
std::stringstream WordTrie::AllMessages()
{
std::stringstream out;
for (WordTrie * x : childNodes)
{
out << x->GetMessages();
}
return out;
}
std::string WordTrie::GetReplies()
{
std::stringstream out;
for (WordTrie * x : childNodes)
{
out << x->GetReplies();
}
if (reply.size() > 3) out << reply << std::endl;
return out.str();
}
std::string WordTrie::GetMessages()
{
std::stringstream out;
for (WordTrie * x : childNodes)
{
out << x->GetMessages();
}
if(reply.size() > 3) out << message << std::endl;
return out.str();
}