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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const ll base = 727;
int main(){
ll n;
string s;
cin >> n >> s;
if (n % 2 == 0){
cout << "NOT POSSIBLE" << endl;
return 0;
}
ll k = n / 2;
ll hsh = 0;
ll pw[n];
pw[0] = 1;
for (ll i = 1; i < n; i ++)
pw[i] = (pw[i - 1] * base) % mod;
ll pref = 0;
ll suff = 0;
ll poss = 0;
string P, S;
for (ll i = 0; i < k; i ++)
pref = (pref * base + s[i]) % mod, P += s[i];
for (ll i = k + 1; i < n; i ++)
suff = (suff * base + s[i]) % mod, S += s[i];
for (ll i = 1; i <= k; i ++)
hsh = (hsh * base + s[i]) % mod;
for (ll i = 0; i < k; i ++){
poss += (hsh == suff);
hsh += pw[k - i - 1] * s[i];
hsh = ((hsh % mod) + mod) % mod;
hsh -= pw[k - i - 1] * s[i + 1];
hsh = ((hsh % mod) + mod) % mod;
}
string ans;
if (poss)
ans = S;
ll nposs = 0;
hsh = suff;
for (ll i = k, j = k - 1; i + 1 < n; i ++, j--){
nposs += (hsh == pref);
hsh -= s[i + 1] * pw[j];
hsh = ((hsh % mod) + mod) % mod;
hsh += s[i] * pw[j];
hsh = ((hsh % mod) + mod) % mod;
}
nposs += (hsh == pref);
if (nposs)
ans = P;
poss += nposs;
if (poss > 1)
cout << "NOT UNIQUE" << endl;
else if (poss == 0)
cout << "NOT POSSIBLE" << endl;
else
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |