This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of God
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6+5;
const int mod = 1e9+7;
const long long p = 37;
long long pw[N], invpw[N];
long long fast_pw(long long x, long long y) {
long long ret = 1;
for(;y;y>>=1) {
if(y & 1) ret = (ret * x) % mod;
x = (x * x) % mod;
}
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
pw[0] = 1;
invpw[0] = 1;
invpw[1] = fast_pw(p, mod-2);
for(int i = 1; i < N; i++) pw[i] = (pw[i-1] * p) % mod;
for(int i = 2; i < N; i++) invpw[i] = (invpw[i-1] * invpw[1]) % mod;
int n;
cin >> n;
string s;
cin >> s;
if(n % 2 == 0) {
cout << "NOT POSSIBLE";
exit(0);
}
int ans = 0;
vector<long long> hs(n, 0);
for(int i = 0; i < n; i++) {
hs[i] = ((s[i] - 'A' + 1) * pw[i]) % mod;
if(i) hs[i] = (hs[i] + hs[i-1]) % mod;
}
auto find_hs = [&](int l, int r) {
// returns the hash of (l, r)
long long ret = hs[r];
if(l) ret = (ret - hs[l - 1] + mod) % mod;
ret = (ret * invpw[l]) % mod;
return ret;
};
auto find_hs_x = [&](int l, int r, int x) {
//returns hash of (l, r) except character x
if(x < l || x > r) return find_hs(l, r);
long long ret = x != l ? find_hs(l, x - 1) : 0;
long long right = x != r ? find_hs(x+1, r) : 0;
ret = (ret + (pw[x-l] * right) % mod ) % mod;
return ret;
};
unordered_set<int> se;
for(int i = 0; i < n; i++) {
long long left,right;
if(i < n / 2) {
left = find_hs_x(0, n / 2, i);
right = find_hs(n / 2 + 1, n - 1);
}
else if(i == n / 2) {
left = find_hs(0, n / 2 - 1);
right = find_hs(n / 2 + 1, n - 1);
}
else {
left = find_hs(0, n / 2 - 1);
right = find_hs_x(n / 2, n - 1, i);
}
if(left == right) {
se.insert(find_hs_x(0, n-1, i));
}
}
if(se.size() == 0) cout << "NOT POSSIBLE";
if(se.size() > 1) cout << "NOT UNIQUE";
if(se.size() == 1) {
for(int i = 0; i < n; i++) {
long long left,right;
if(i < n / 2) {
left = find_hs_x(0, n / 2, i);
right = find_hs(n / 2 + 1, n - 1);
}
else if(i == n / 2) {
left = find_hs(0, n / 2 - 1);
right = find_hs(n / 2 + 1, n - 1);
}
else {
left = find_hs(0, n / 2 - 1);
right = find_hs_x(n / 2, n - 1, i);
}
if(left == right) {
string ans = "";
int pos = 0;
while(ans.size()!=n/2) {
if(pos==i) {
pos++;
continue;
}
ans += s[pos];
pos++;
}
cout << ans;
exit(0);
}
}
}
return 0;
}
Compilation message (stderr)
friends.cpp: In function 'int main()':
friends.cpp:116:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ans.size()!=n/2) {
~~~~~~~~~~^~~~~
friends.cpp:47:6: warning: unused variable 'ans' [-Wunused-variable]
int ans = 0;
^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |