-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
199 lines (164 loc) · 6.23 KB
/
main.cpp
File metadata and controls
199 lines (164 loc) · 6.23 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
/*
Finding longest valid word in a string in linear time
Elijah Moreau-Arnott
Please be aware that I made this in 3 hours... there may be spelling mistakes...
I would perfect it more (I know a good impression on an imployer is a good thing)
but unfortunatly I have to write a bio essay and finish my SE Quality Assurance assignment
***********************************/
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
//cheep defines to amke "assemble dictionary" look better... In the future I would make the
//'next' pointers in the kletter struct private, and have a getter function that does this translation
#define C 0
#define I 1
#define K 2
#define L 3
#define P 4
#define S 5
#define T 6
#define Y 7
/*We will use a subset of the english alphabet containing the letetrs
{C,I,K,L,P,S,T,Y}
and a subset of the english dictionary containing the words
{"clip", "clips", "lip", "lips", "lipstick", "stick", "sticky", "tic", "tick", "i", "sick", "slip", "slips"}
*/
const char alphabet[] = {'c','i','k','l','p','s','t','y'};
const int alphabetLength = 8;
//global final variables
string longestWord;
int ofLength;
struct letter{
letter* next[alphabetLength];
int level;
bool done;
letter(int level,bool wholeWord): level(level), done(wholeWord){
for(int i=0;i<alphabetLength;i++){
next[i] = NULL;
}
}
};
//cheep translation to make the code look better later
//with a real alphabet this wouldnt be needed
int asciiToAlphabet(int charCode){
switch(charCode){
case 67://C
return 0;
case 73://I
return 1;
case 75://K
return 2;
case 76://L
return 3;
case 80://P
return 4;
case 83://S
return 5;
case 84://T
return 6;
case 89://Y
return 7;
default:
cerr << endl << "Error: Letter not in alphabet!" << endl;
exit(1);
}
}
//For assembling the dictionary's nodes into a sufix tree
//This removes all null nodes by pointing all null nodes back to starting characters
void nullDictionary(letter* node, letter* start, int hi){
for(int i=0;i<alphabetLength;i++){
if( node->next[i] == NULL ){
node->next[i] = start->next[i];
}
else{
nullDictionary(node->next[i], start, ++hi);
}
}
}
void assembleDictionary(letter** head){
letter* start = new letter(0,true);
*head = start;
start->next[C] = new letter(1,false);
start->next[C]->next[L] = new letter(2,false);
start->next[C]->next[L]->next[I] = new letter(3,false);
start->next[C]->next[L]->next[I]->next[P] = new letter(4,true);
start->next[C]->next[L]->next[I]->next[P]->next[S] = new letter(5,true);
start->next[L] = new letter(1,false);
start->next[L]->next[I] = new letter(2,false);
start->next[L]->next[I]->next[P] = new letter(3,true);
start->next[L]->next[I]->next[P]->next[S] = new letter(4,true);
start->next[L]->next[I]->next[P]->next[S]->next[T] = new letter(5,false);
start->next[L]->next[I]->next[P]->next[S]->next[T]->next[I] = new letter(6,false);
start->next[L]->next[I]->next[P]->next[S]->next[T]->next[I]->next[C] = new letter(7,false);
start->next[L]->next[I]->next[P]->next[S]->next[T]->next[I]->next[C]->next[K] = new letter(8,true);
start->next[S] = new letter(1,false);
start->next[S]->next[T] = new letter(2,false);
start->next[S]->next[T]->next[I] = new letter(3,true);
start->next[S]->next[T]->next[I]->next[C] = new letter(4,false);
start->next[S]->next[T]->next[I]->next[C]->next[K] = new letter(5,true);
start->next[S]->next[T]->next[I]->next[C]->next[K]->next[Y] = new letter(6,true);
start->next[T] = new letter(1,false);
start->next[T]->next[I] = new letter(2,false);
start->next[T]->next[I]->next[C] = new letter(3,true);
start->next[T]->next[I]->next[C]->next[K] = new letter(4,true);
//added a couple words for testing
start->next[S]->next[L] = new letter(2,false);
start->next[S]->next[L]->next[I] = new letter(3,false);
start->next[S]->next[L]->next[I]->next[P] = new letter(4,true);
start->next[S]->next[L]->next[I]->next[P]->next[S] = new letter(5,true);
start->next[S]->next[I] = new letter(2,false);
start->next[S]->next[I]->next[C] = new letter(3,false);
start->next[S]->next[I]->next[C]->next[K] = new letter(4,true);
//now we have tree'd dictionary..what we may expect in isWord()
//create all start nodes
start->next[I] = new letter(1,true);
start->next[K] = new letter(1,false);
start->next[P] = new letter(1,false);
start->next[Y] = new letter(1,false);
//Now we link, which is the special part
//set all null links back to the start, so that there are no null links
nullDictionary(start, start, 0);
//now add some subset linking, this is dictionary dependant but could be automated
//
start->next[L]->next[I]->next[P]->next[S]->next[T]->next[I]->next[C]->next[K]->next[Y] = start->next[S]->next[T]->next[I]->next[C]->next[K]->next[Y];
start->next[C]->next[L]->next[I]->next[P]->next[S]->next[T] = start->next[L]->next[I]->next[P]->next[S]->next[T];
start->next[S]->next[L]->next[I]->next[P]->next[S]->next[T] = start->next[S]->next[T];
start->next[S]->next[I]->next[C]->next[L] = start->next[C]->next[L];
start->next[S]->next[L]->next[I]->next[P]->next[S]->next[I] = start->next[S]->next[I];
start->next[C]->next[L]->next[I]->next[P]->next[S]->next[I] = start->next[S]->next[I];
start->next[L]->next[I]->next[P]->next[S]->next[I] = start->next[S]->next[I];
start->next[L]->next[I]->next[P]->next[S]->next[L] = start->next[S]->next[L];
}
void findString(string making, string full, letter* dictionary){
cout << making << "|" << full << endl;
if(dictionary->done){
if(making.length() != dictionary->level){
making.erase(making.begin(),making.begin()+making.length()-dictionary->level);
}
cout << "Word Found = " << making << " of length " << dictionary->level << endl;
if(dictionary->level > ofLength){
longestWord = making;
ofLength = dictionary->level;
}
}
if(full.length() == 0)
{
return;
}
char first = full[0];
making.push_back(full[0]);
full.erase(full.begin());
findString(making,full,dictionary->next[asciiToAlphabet(first)]);
}
int main () {
longestWord = "";
ofLength = 0;
letter* dictionary;
assembleDictionary(&dictionary);
//string use = "ITICSLIPSICKT";
string use = "CLIPSTICKY";
findString("", use, dictionary);
cout << "*******" << endl << "The longest word is " << longestWord << " which is of length " << ofLength << endl;
return 0;
}