forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypo.cpp
105 lines (88 loc) · 2.08 KB
/
typo.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll smallprime = 31;
ll largeprime1 = 1610612741;
ll largeprime2 = 201326611;
ll fastpow(ll base, ll exp, ll mod) {
base %= mod;
exp %= mod;
ll res = 1;
while(exp > 0) {
if((exp & 1) == 1) {
res = (res * base) % mod;
}
base = (base * base) % mod;
exp /= 2;
}
return res % mod;
}
ll hashstr(string& s, ll largeprime) {
ll res = 0;
for(ll i = 0; i < s.size(); i++) {
res *= smallprime;
res %= largeprime;
res += s[i] - 'a' + 1;
res %= largeprime;
}
return res;
}
vector<ll> getsubhash(string& s, ll largeprime) {
vector<ll> subhashes;
// The Ith character is the one being ignored
ll l = 0;
ll r = 0;
for(ll i = 0; i < s.size(); i++) {
r *= smallprime;
r %= largeprime;
r += s[i] - 'a' + 1;
r %= largeprime;
}
for(ll i = 0; i < s.size(); i++) {
// Remove from the front of R
r -= (fastpow(smallprime, (s.size()-i-1), largeprime) * (s[i] - 'a' + 1)) % largeprime;
while(r < 0) r += largeprime;
// Build string from the two
ll mid = r + (l * fastpow(smallprime, (s.size()-i-1), largeprime)) % largeprime;
mid %= largeprime;
subhashes.push_back(mid);
// Debugging
//cout << s << " missing char " << i << " hashes to " << mid << endl;
// Add to end of L
l *= smallprime;
l %= largeprime;
l += s[i] - 'a' + 1;
l %= largeprime;
}
return subhashes;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n;
cin >> n;
set<pair<ll,ll>> words;
vector<string> v(n);
for(auto& i : v) {
cin >> i;
pair<ll,ll> val = {hashstr(i,largeprime1),hashstr(i,largeprime2)};
words.insert(val);
//cout << i << " hashed to " << hashstr(i) << endl;
}
bool printed = false;
for(auto i : v) {
vector<ll> subhash1 = getsubhash(i,largeprime1);
vector<ll> subhash2 = getsubhash(i,largeprime2);
for(int j = 0; j < subhash1.size(); j++) {
if(words.count({subhash1[j],subhash2[j]}) > 0) {
cout << i << endl;
printed = true;
break;
}
}
}
if(!printed) {
cout << "NO TYPOS" << endl;
}
}