-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp_main.cpp
More file actions
200 lines (146 loc) · 3.96 KB
/
p_main.cpp
File metadata and controls
200 lines (146 loc) · 3.96 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <thread>
#include "main.h"
int word_index; // INDEX position in the vector array to be shared by the threads
mutex mtx;
Trie* trie_root;
vector<string> v;
void Trie_init(Trie* t){
for(int i=0;i<NO_OF_APLHABETS;i++){
t->children[i]=NULL;
}
t->isEnd = false;
}
void Trie_add(Trie* t,string str){
for(int i=0;i<str.length();i++){
if(t->children[str[i]-LOWEST_ASCII_ALPHABET]==NULL){
t->children[str[i]-LOWEST_ASCII_ALPHABET]=new Trie;
t=t->children[str[i]-LOWEST_ASCII_ALPHABET];
Trie_init(t);
}else{
t=t->children[str[i]-LOWEST_ASCII_ALPHABET];
}
}
t->isEnd=true;
}
// word_no for the ith word
// letter in the word_no
void checkSolution(Trie *t, Trie** pointers, int word_no, int depth, vector<string> &sol){
// cout<<"Word_no: "<<word_no<<"\tDepth: "<<depth<<endl;
if(depth==SQUARE_SIZE){
depth = word_no+1;
word_no++;
}
if(word_no==SQUARE_SIZE){
mtx.lock();
// cout<<"---------------------------- Solution found ----------------------------"<<endl;
for(int i=0;i<sol.size();i++){
cout<<sol[i]<<endl;
}
cout<<endl;
// cout<<"------------------------------------------------------------------------"<<endl;
mtx.unlock();
return;
}
for(int i=0;i<NO_OF_APLHABETS;i++){
if(pointers[word_no]->children[i]==NULL){ // solution doesn't exist with the current word
continue;
}else{
if(pointers[depth]->children[i]==NULL){ // solution doesn't exist with the depth index word
continue;
}
Trie* cur_word_pointer = pointers[word_no];
Trie* depth_word_pointer = pointers[depth];
pointers[word_no] = pointers[word_no]->children[i];
if(word_no!=depth){
pointers[depth] = pointers[depth]->children[i];
}
sol[word_no][depth] = 'a'+i;
sol[depth][word_no] = 'a'+i;
// recursive call for the next letter
checkSolution(t,pointers,word_no,depth+1,sol);
pointers[word_no] = cur_word_pointer;
if(word_no!=depth){
pointers[depth] = depth_word_pointer;
}
}
}
}
void threadHelper(int thread_id){
Trie *t = trie_root;
int k = SQUARE_SIZE;
int flag;
Trie** pointers = new Trie*[k];
string str;
vector<string> sol(k);
for(int i=0;i<sol.size();i++){
sol[i] = v[0];
}
while(1){
mtx.lock();
if(word_index==v.size()){
return;
}
str = v[word_index];
word_index++;
// cout << "thread #" << thread_id << " checking for solutions beginning with : "<<str<<endl;
// cout<< "thread #" << thread_id << " word_index "<<word_index-1<<endl;
mtx.unlock();
sol[0]=str;
// inefficient memory access, column wise TO DO
for(int j=0;j<str.length();j++){
sol[j][0] = str[j];
}
flag = 1;
for(int i=0;i<str.length();i++){
if(t->children[str[i]-LOWEST_ASCII_ALPHABET]==NULL){ // No solution exists with the current string
flag = 0;
break;
}
pointers[i] = t->children[str[i]-LOWEST_ASCII_ALPHABET];
}
if(flag){
// There might be a possible solution with the current string, need to check
// cout<<"Checking for solution for words beginning with : "<<str<<endl;
checkSolution(t,pointers,1,1,sol);
}else{
// cout<<"No solution for words beginning with : "<<str<<endl;
}
}
}
// Using k pointers in trie approach approach
void kPointersTrie(Trie* t){
// cout<<Trie_search(t,"ace")<<endl;
int flag;
if(v.size()<SQUARE_SIZE){
// cout<<"Not enough strings given"<<endl;
}
thread threads[NO_OF_THREADS];
for(int i=0;i<NO_OF_THREADS;i++){
threads[i] = thread(threadHelper,i);
}
for(int i=0;i<NO_OF_THREADS;i++){
threads[i].join();
}
}
int main(){
freopen(FILE_NAME,"r",stdin);
string str;
Trie* root = new Trie;
trie_root = root;
Trie_init(root);
word_index = 0;
int no_of_words;
cin>>no_of_words;
assert(SQUARE_SIZE>0);
assert(no_of_words>=SQUARE_SIZE);
for(int i=0;i<no_of_words;i++){
cin>>str;
assert(str.length()==SQUARE_SIZE);
transform(str.begin(),str.end(),str.begin(),::tolower);
v.push_back(str);
Trie_add(root,str);
}
sort(v.begin(),v.end());
kPointersTrie(root);
return 0;
}