-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordFinder.cpp
More file actions
169 lines (151 loc) · 3.83 KB
/
wordFinder.cpp
File metadata and controls
169 lines (151 loc) · 3.83 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
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
class Dictionary
{
private:
std::unordered_set<std::string> words;
public:
Dictionary()
{}
bool loadFromFile(std::string file_name)
{
std::ifstream infile(file_name.c_str());
if (!infile.is_open())
{
std::cout << file_name << " is not opened." << std::endl;
return false;
}
//just push_back
std::string line;
while(getline(infile, line))
{
std::transform(line.begin(), line.end(), line.begin(), ::tolower);
words.insert(line);
}
infile.close();
return true;
}
bool checkExist(std::string word)
{
return words.find(word) != words.end();
}
};
void rec_permutation(const std::vector<char>& chars, std::vector<char>& chars_so_far,
size_t cur_index, size_t min_char_size,
std::vector<size_t>& visited, std::unordered_set<size_t>& unvisited,
std::unordered_set<std::string>& words)
{
//add the current char to chars_so_far and add words
chars_so_far.push_back(chars[cur_index]);
if (chars_so_far.size() >= min_char_size)
words.insert(std::string(chars_so_far.begin(), chars_so_far.end()));
//if the last letter
if (unvisited.size() == 1)
{
chars_so_far.pop_back();
return;
}
else
{
visited.push_back(cur_index);
unvisited.erase(cur_index);
std::vector<size_t> cur_unvisited(unvisited.begin(), unvisited.end());
for (size_t i: cur_unvisited)
{
rec_permutation(chars, chars_so_far,
i, min_char_size,
visited, unvisited,
words);
}
unvisited.insert(cur_index);
visited.pop_back();
chars_so_far.pop_back();
return;
}
}
void createPermutations(const std::vector<char>& chars, size_t min_char_size, std::unordered_set<std::string>& words)
{
std::vector<char> chars_so_far;
std::vector<size_t> visited;
std::unordered_set<size_t> unvisited;
for (size_t i = 0; i < chars.size(); ++i)
{
unvisited.insert(i);
}
for (size_t i = 0; i < chars.size(); ++i)
{
rec_permutation(chars, chars_so_far, i, min_char_size, visited, unvisited, words);
}
}
int main(int argv, char* argc[])
{
if (argv < 2)
{
std::cout << "need to enter at least 2 charactors" << std::endl;
return 0;
}
int min_char_size = (int)argc[1][0] - 48;
//get charactors
std::vector<char> chars;
std::string arg_str = argc[2];
for (size_t i = 0; i < arg_str.length(); ++i)
{
chars.push_back(std::tolower(arg_str[i]));
}
// for (int i = 2; i < argv; ++i)
// {
// std::string str = argc[i];
// chars.push_back(std::tolower(str[0]));
// }
std::unordered_set<std::string> words;
createPermutations(chars, (size_t)min_char_size, words);
Dictionary dict;
std::string file_name = "dictionary.txt";
dict.loadFromFile(file_name);
std::unordered_map<size_t, std::vector<std::string> > true_words;
std::vector<size_t> lengths;
for (std::string w: words)
{
if(dict.checkExist(w))
{
size_t len = w.length();
if (true_words.find(len) == true_words.end())
{
lengths.push_back(len);
std::vector<std::string> temp;
temp.push_back(w);
true_words[len] = temp;
}
else
{
true_words[len].push_back(w);
}
}
}
std::sort(lengths.begin(), lengths.end());
std::ofstream outfile("found_words.txt");
size_t total_num_words = 0;
for (size_t i : lengths)
{
std::vector<std::string> temp = true_words[i];
std::sort(temp.begin(), temp.end());
outfile << "length[" << i << "]: " << temp.size() << "\n";
std::cout << "length[" << i << "]: " << temp.size() << std::endl;
total_num_words += temp.size();
for (std::string str: temp)
{
outfile << str << "\n";
std::cout << str << "\n";
}
outfile << "\n";
std::cout << std::endl;
}
outfile << "total number of words: " <<total_num_words << std::endl;
std::cout << "total number of words: " <<total_num_words << std::endl;
outfile.close();
return 0;
}