This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 startingLetter = 'A'; // starting letter of the alphabet
ll hashChar(char c) {
return c - startingLetter + 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 = "", right = "";
for (int i = 0; i < n / 2 + 1; i++)
left += u[i];
for (int i = n / 2 + 1; i < n; i++)
right += u[i];
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 (int i = 1; i < left.size(); i++) {
auto d = hshLeft.back();
hshLeft.push_back(hashStr(d, left[i]));
}
hshRight.push_back({hashChar(right[0]), hashChar(right[0])});
for (int i = 1; i < right.size(); i++) {
auto d = hshRight.back();
hshRight.push_back(hashStr(d, right[i]));
}
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 == "") {
for (int j = 0; j < right.size(); j++)
if (i != j)
ans += right[j];
for (char j : ans)
ansHsh = hashStr(ansHsh, j);
} 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 == "") {
for (int j = 0; j < left.size(); j++)
if (i != j)
ans += left[j];
for (char j : ans)
ansHsh = hashStr(ansHsh, j);
} 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 (stderr)
friends.cpp: In function 'str findUniqueString(int, const str&)':
friends.cpp:52:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 1; i < left.size(); i++) {
| ~~^~~~~~~~~~~~~
friends.cpp:58:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 1; i < right.size(); i++) {
| ~~^~~~~~~~~~~~~~
friends.cpp:64:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i = 1; i < right.size(); i++) {
| ~~^~~~~~~~~~~~~~
friends.cpp:75:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int j = 0; j < right.size(); j++)
| ~~^~~~~~~~~~~~~~
friends.cpp:87:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = 0; i < left.size(); i++) {
| ~~^~~~~~~~~~~~~
friends.cpp:98:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (int j = 0; j < left.size(); j++)
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |