#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef string str;
typedef vector<ll> vint;
const ll MOD1 = 1999998893;
const ll MOD2 = 2100003611;
const ll BASE = 29;
const char STARTING_LETTER = 'A';
ll hashChar(char c) {
return c - STARTING_LETTER + 1;
}
pair<ll, ll> hashStr(pair<ll, ll> prevHash, char curChar) {
return {((prevHash.first * BASE) % MOD1 + hashChar(curChar)) % MOD1,
((prevHash.second * BASE) % MOD2 + hashChar(curChar)) % MOD2};
}
void calculatePowers(ll powOfBase1[], ll powOfBase2[], int n) {
powOfBase1[0] = 1;
powOfBase2[0] = 1;
for (int i = 1; i < n; i++) {
powOfBase1[i] = (BASE * powOfBase1[i - 1]) % MOD1;
powOfBase2[i] = (BASE * powOfBase2[i - 1]) % MOD2;
}
}
str findUniqueString(int n, const str& u) {
str left = u.substr(0, n / 2 + 1);
str right = u.substr(n / 2 + 1);
str ans = "";
pair<ll, ll> ansHsh = {0, 0};
ll powOfBase1[n], powOfBase2[n];
calculatePowers(powOfBase1, powOfBase2, n);
for (int z = 0; z < 2; z++) {
vector<pair<ll, ll>> hshLeft, hshRight;
hshLeft.push_back({hashChar(left[0]), hashChar(left[0])});
for (char c : left.substr(1)) {
auto d = hshLeft.back();
hshLeft.push_back(hashStr(d, c));
}
hshRight.push_back({hashChar(right[0]), hashChar(right[0])});
for (char c : right.substr(1)) {
auto d = hshRight.back();
hshRight.push_back(hashStr(d, c));
}
if (z) {
for (int i = 1; i < right.size(); i++) {
auto resultHash = hshRight.back();
int offset = right.size() - (i + 1);
resultHash.first = ((resultHash.first - (powOfBase1[offset] * hshRight[i].first) % MOD1) % MOD1 + MOD1) % MOD1;
if (i)
resultHash.first = (resultHash.first + (powOfBase1[offset] * hshRight[i - 1].first) % MOD1) % MOD1;
resultHash.second = ((resultHash.second - (powOfBase2[offset] * hshRight[i].second) % MOD2) % MOD2 + MOD2) % MOD2;
if (i)
resultHash.second = (resultHash.second + (powOfBase2[offset] * hshRight[i - 1].second) % MOD2) % MOD2;
if (resultHash == hshLeft.back()) {
if (ans.empty()) {
for (int j = 0; j < right.size(); j++)
if (i != j)
ans += right[j];
for (char c : ans)
ansHsh = hashStr(ansHsh, c);
} else {
if (resultHash != ansHsh)
return "NOT UNIQUE";
}
}
}
} else {
for (int i = 0; i < left.size(); i++) {
auto resultHash = hshLeft.back();
int offset = left.size() - (i + 1);
resultHash.first = ((resultHash.first - (powOfBase1[offset] * hshLeft[i].first) % MOD1) % MOD1 + MOD1) % MOD1;
if (i)
resultHash.first = (resultHash.first + (powOfBase1[offset] * hshLeft[i - 1].first) % MOD1) % MOD1;
resultHash.second = ((resultHash.second - (powOfBase2[offset] * hshLeft[i].second) % MOD2) % MOD2 + MOD2) % MOD2;
if (i)
resultHash.second = (resultHash.second + (powOfBase2[offset] * hshLeft[i - 1].second) % MOD2) % MOD2;
if (resultHash == hshRight.back()) {
if (ans.empty()) {
for (int j = 0; j < left.size(); j++)
if (i != j)
ans += left[j];
for (char c : ans)
ansHsh = hashStr(ansHsh, c);
} else {
if (resultHash != ansHsh)
return "NOT UNIQUE";
}
}
}
}
if (z)
break;
right = left.back() + right;
left.pop_back();
}
if (ans.empty())
return "NOT POSSIBLE";
else
return ans;
}
int main() {
srand(time(0));
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(15);
int n;
cin >> n;
str u;
cin >> u;
str result = findUniqueString(n, u);
cout << result;
return 0;
}
Compilation message
friends.cpp: In function 'str findUniqueString(int, const str&)':
friends.cpp:61:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 1; i < right.size(); i++) {
| ~~^~~~~~~~~~~~~~
friends.cpp:72:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int j = 0; j < right.size(); j++)
| ~~^~~~~~~~~~~~~~
friends.cpp:84:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for (int i = 0; i < left.size(); i++) {
| ~~^~~~~~~~~~~~~
friends.cpp:95:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for (int j = 0; j < left.size(); j++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
500 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
600 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
344 KB |
Output is correct |
51 |
Correct |
1 ms |
344 KB |
Output is correct |
52 |
Correct |
1 ms |
344 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
86320 KB |
Output is correct |
2 |
Correct |
156 ms |
86288 KB |
Output is correct |
3 |
Correct |
151 ms |
86160 KB |
Output is correct |
4 |
Correct |
149 ms |
86048 KB |
Output is correct |
5 |
Correct |
150 ms |
87168 KB |
Output is correct |
6 |
Correct |
135 ms |
85844 KB |
Output is correct |
7 |
Correct |
158 ms |
86992 KB |
Output is correct |
8 |
Correct |
135 ms |
80316 KB |
Output is correct |
9 |
Correct |
138 ms |
82060 KB |
Output is correct |
10 |
Correct |
136 ms |
81912 KB |
Output is correct |
11 |
Correct |
114 ms |
79732 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
544 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |