-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (119 loc) · 3.7 KB
/
main.cpp
File metadata and controls
131 lines (119 loc) · 3.7 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
#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include <unistd.h>
#include <chrono>
using namespace std;
const int ALPHABET_SIZE = 26;
vector<string> Dict;
//Start of GeeksForGeeks Implementation Link: https://www.geeksforgeeks.org/trie-insert-and-search/
struct Node{
struct Node *children[ALPHABET_SIZE];
bool isEndOfWord;
};
struct Node *getNode(void){
struct Node *parent = new Node;
parent->isEndOfWord = false;
for(int i = 0;i < ALPHABET_SIZE;i++){
parent->children[i] = NULL;
}
return parent;
}
void insert(struct Node *root,string key){
struct Node *pCrawl = root;
for (int i = 0; i < key.length(); i++){
int index = key[i] - 'a';
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
// mark last node as leaf
pCrawl->isEndOfWord = true;
}
bool search(struct Node *root, string key)
{
struct Node *pCrawl = root;
for (int i = 0; i < key.length(); i++)
{
int index = key[i] - 'a';
if (!pCrawl->children[index])
return false;
pCrawl = pCrawl->children[index];
}
return (pCrawl->isEndOfWord);
}
//End of GeeksForGeeks Implementation
//randomly creating string of a specific length
string genSequence(int length){
string ret;
char chars[] = "abcdefghijklmnopqrstuvwxyz";
srand( (unsigned) time(NULL) * getpid());
ret.reserve(length);
for (int i = 0; i < length; ++i){
ret += chars[rand() % (sizeof(chars)-1)];
}
cout << ret << endl;
return ret;
}
//Further Research Needed: https://en.wikipedia.org/wiki/Aho–Corasick_algorithm
//This function works, recursively goes throught the trie
void subsetSearch(struct Node* root,string s,bool visited[],string newStr){
struct Node* tt = root;
//If its the end of a word, then add it to the collection of valid words
if(tt->isEndOfWord){
cout << newStr << endl;
}
for(int i = 0;i < s.length();i++){
//go through all of the remaining chars, if they haven't been visited then add
//them to the string
if(visited[i] == false){
int index = s[i] - 'a';
if(!tt->children[index]){
continue;
}
newStr += s[i];
visited[i] = true;
Node* xx = tt->children[index];
subsetSearch(xx,s,visited,newStr);
visited[i] = false;
newStr = newStr.substr(0,newStr.length()-1);
}
}
}
int main(){
//reading file and pushing content to vector Dict
ifstream file;
file.open("WordList.txt");
string output;
if(file.is_open()){
while(!file.eof()){
file >> output;
Dict.push_back(output);
}
}
//creating tree structure
int Dict_size = Dict.size();
struct Node *root = getNode();
for(int i = 0;i < Dict_size;i++){
insert(root,Dict[i]);
}
//creating a random string of x length
int StringLength = 30;
//string testWord = genSequence(StringLength);
string testWord = "oifpyrki";
auto t1 = std::chrono::high_resolution_clock::now();
//Searching for all substrings Function
for(int i = 0;i < testWord.length();i++){
bool visited[testWord.length()];
string newStr = "";
newStr += testWord[i];
int index = testWord[i] - 'a';
Node* temp = root->children[index];
visited[i] = true;
subsetSearch(temp,testWord,visited,newStr);
}
auto t2 = std::chrono::high_resolution_clock::now();
cout << "Microseconds: " << std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count() << endl;
cout << "ppopp" << endl;
}