-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomaticCorrectionOfMisspellings_UVA11048.cpp
122 lines (106 loc) · 2.51 KB
/
automaticCorrectionOfMisspellings_UVA11048.cpp
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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
ifstream fin("automaticCorrectionOfMisspellings_UVA11048.in");
ofstream fout("automaticCorrectionOfMisspellings_UVA11048.out");
bool check(string & a, string & b)
{
int sizeA = a.size(), sizeB = b.size();
if (abs(sizeA - sizeB) == 1)
{
string tempA, tempB;
if (sizeA > sizeB)
{
tempA = b;
tempB = a;
}
else
{
tempA = a;
tempB = b;
}
int size = max(sizeA, sizeB);
for (int i = 0; i <= size - 1; ++i)
{
string temp = tempB;
temp.erase(temp.begin() + i);
if (tempA == temp)
{
return true;
}
}
return false;
}
else if (sizeA == sizeB)
{
bool flag = false;
for (int i = 0; i <= sizeA - 1; ++i)
{
if (a[i] != b[i])
{
if (flag == true)
{
return false;
}
if (i != sizeA - 1)
{
if (a[i] == b[i + 1] && a[i + 1] == b[i])
{
++i;
}
}
flag = true;
}
}
return true;
}
else
{
return false;
}
}
int main()
{
int n; fin >> n;
map<string, bool> dictionary_map;
vector<string> dictionary_vec(n);
for (int i = 0; i <= n - 1; ++i)
{
fin >> dictionary_vec[i];
dictionary_map[dictionary_vec[i]] = true;
}
int q; fin >> q;
for (int c = 1; c <= q; ++c)
{
string s; fin >> s;
fout << s << ' ';
if (dictionary_map[s] == true)
{
fout << "is correct\n";
continue;
}
bool flag = true;
for (int i = 0; i <= n - 1 && flag == true; ++i)
{
string now = dictionary_vec[i];
if (check(s, now) == true)
{
fout << "is a misspelling of " << now << '\n';
flag = false;
}
}
if (flag == true)
{
fout << "is unknown\n";
}
}
return 0;
}