-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCL1-1.cpp
More file actions
195 lines (179 loc) · 5.83 KB
/
CL1-1.cpp
File metadata and controls
195 lines (179 loc) · 5.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
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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>
using namespace std;
const char* INPUT_FILENAME = "input.txt";
const char* OUTPUT_FILENAME = "output.txt";
const string RUSSIAN_ALPHABET = "àáâãä叿çèéêëìíîïðñòóôõö÷øùúûüýþÿ"
"ÀÁÂÃÄŨÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞß";
typedef vector<string> Dictionary;
typedef vector<string> WordList;
typedef pair<string, int> SearchRecord;
typedef vector<SearchRecord> SearchDictionary;
void ReadDictionary(istream& inputStream, Dictionary* dictionary)
{
int dictSize;
inputStream >> dictSize >> ws;
dictionary->resize(dictSize);
for (int i = 0; i < dictSize; ++i) {
getline(inputStream, dictionary->at(i));
}
}
void FindNewWords(const string& word, WordList* newWords, int nLettersToRemove,
const string& prefix = "")
{
if (nLettersToRemove == 0 || word.length() == 0) {
newWords->push_back(prefix + word);
} else {
// We can either delete first letter
FindNewWords(word.substr(1), newWords, nLettersToRemove - 1, prefix);
// or not
FindNewWords(word.substr(1), newWords, nLettersToRemove, prefix + word[0]);
}
// Leave only one copy of each string
sort(newWords->begin(), newWords->end());
newWords->resize(
unique(newWords->begin(), newWords->end()) - newWords->begin());
}
void CreateSearchDictionary(const Dictionary& dict,
SearchDictionary* searchDict, int nLettersToRemove)
{
searchDict->clear();
for (size_t iWord = 0; iWord < dict.size(); ++iWord) {
WordList newWords;
FindNewWords(dict[iWord], &newWords, nLettersToRemove);
for (size_t iNewWord = 0; iNewWord < newWords.size(); ++iNewWord) {
searchDict->push_back(make_pair(newWords[iNewWord], iWord));
}
}
sort(searchDict->begin(), searchDict->end());
}
bool ReadWord(istream& inputStream, string* word)
{
word->assign("");
bool isWord = true;
char c;
while ((c = inputStream.peek()) && isWord && !inputStream.eof()) {
isWord = (RUSSIAN_ALPHABET.find(c) != string::npos);
if (isWord) {
word->push_back(c);
inputStream.get();
}
}
return (word->size() > 0);
}
bool ReadExtraChars(istream& inputStream, string* extraChars)
{
extraChars->assign("");
bool isExtraChar = true;
char c;
while ((c = inputStream.peek()) && isExtraChar && !inputStream.eof()) {
isExtraChar = (RUSSIAN_ALPHABET.find(c) == string::npos);
if (isExtraChar) {
extraChars->push_back(c);
inputStream.get();
}
}
return (extraChars->size() > 0);
}
int LevenshteinDistance(const string& s1, const string& s2) {
const int size1 = (int)s1.size();
const int size2 = (int)s2.size();
vector< vector<int> > matrix(size1 + 1);
for (int i = 0; i <= size1; ++i) {
matrix[i].resize(size2 + 1);
}
for (int i = 0; i <= size1; ++i) {
matrix[i][0] = i;
}
for (int j = 0; j <= size2; ++j) {
matrix[0][j] = j;
}
for (int i = 1; i <= size1; ++i)
for (int j = 1; j <= size2; ++j) {
if (s1[i - 1] == s2[j - 1]) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = min(matrix[i - 1][j - 1],
min(matrix[i - 1][j], matrix[i][j - 1])) + 1;
}
}
return matrix[size1][size2];
}
string FindCorrectedWord(const string& word, const Dictionary& dict,
const SearchDictionary& searchDict, int maxDistance)
{
WordList newWords;
FindNewWords(word, &newWords, maxDistance);
const struct StringComparator {
bool operator() (const SearchRecord& lhs, const SearchRecord& rhs) const {
return lhs.first < rhs.first;
}
} stringComparator;
vector<int> candidates;
for (size_t iWord = 0; iWord < newWords.size(); ++iWord) {
pair<SearchDictionary::const_iterator,
SearchDictionary::const_iterator> bounds =
equal_range(searchDict.begin(), searchDict.end(),
make_pair(newWords[iWord], 0), stringComparator);
while (bounds.first < bounds.second) {
candidates.push_back(bounds.first->second);
++bounds.first;
}
}
// Leave unique candidates
sort(candidates.begin(), candidates.end());
candidates.resize(
unique(candidates.begin(), candidates.end()) - candidates.begin());
vector<int> dictIndexes;
for (size_t i = 0; i < candidates.size(); ++i) {
const string dictWord = dict[candidates[i]];
if (LevenshteinDistance(word, dictWord) <= maxDistance) {
dictIndexes.push_back(candidates[i]);
}
}
string answer;
if (dictIndexes.size() == 0) {
answer = word;
} else if (dictIndexes.size() == 1) {
answer = dict[dictIndexes[0]];
} else {
answer = "{";
for (size_t i = 0; i < dictIndexes.size(); ++i) {
answer += dict[dictIndexes[i]];
if (i < dictIndexes.size() - 1) {
answer += ",";
}
}
answer += "}";
}
return answer;
}
int main()
{
const int maxDistance = 2;
ifstream inputFile(INPUT_FILENAME);
Dictionary dictionary;
ReadDictionary(inputFile, &dictionary);
SearchDictionary searchDictionary;
CreateSearchDictionary(dictionary, &searchDictionary, maxDistance);
//ostream outputFile(cout.rdbuf());
ofstream outputFile(OUTPUT_FILENAME);
while (!inputFile.eof()) {
string extraChars;
if (ReadExtraChars(inputFile, &extraChars)) {
outputFile << extraChars;
}
string word;
if (ReadWord(inputFile, &word)) {
outputFile << FindCorrectedWord(
word, dictionary, searchDictionary, maxDistance);
}
}
outputFile.close();
inputFile.close();
return 0;
}