# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
284783 | ChrisT | Three Friends (BOI14_friends) | C++17 | 134 ms | 20984 KiB |
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;
const int MN = 2e6 + 5, MOD = 1e9 + 9, BASE = 143;
char s[MN]; int pw[MN], hsh[MN];
int substr (int l, int r) {
if (r < l) return 0;
int ret = hsh[r] - (long long)hsh[l-1] * pw[r-l+1] % MOD;
if (ret < 0) ret += MOD;
return ret;
}
int main () {
int n;
scanf ("%d",&n);
scanf ("%s",s+1);
if (!(n&1)) return !printf ("NOT POSSIBLE\n");
pw[0] = 1;
for (int i = 1; i <= n; i++) {
pw[i] = (long long)pw[i-1] * BASE % MOD;
hsh[i] = ((long long)hsh[i-1] * BASE + s[i] - 'A' + 1) % MOD;
}
int has = -1, where = -1;
for (int i = 1; i <= (n+1)/2; i++) {
int lhsh = ((long long)substr(1,i-1) * pw[n/2 - i + 1] + substr(i+1,n/2+1)) % MOD;
int rhsh = substr(n/2+2,n);
if (lhsh == rhsh) {
if (~has && has != lhsh) return !printf ("NOT UNIQUE\n");
has = lhsh; where = i;
}
}
for (int i = n/2+2; i <= n; i++) {
int lhsh = substr(1,n/2);
int rhsh = ((long long)substr((n+1)/2,i-1) * pw[n-i] + substr(i+1,n)) % MOD;
if (lhsh == rhsh) {
if (~has && has != lhsh) return !printf ("NOT UNIQUE\n");
has = lhsh; where = i;
}
}
if (where == -1) return !printf ("NOT POSSIBLE\n");
if (where > n/2) {
for (int i = 1; i <= n/2; i++) printf ("%c",s[i]);
printf ("\n");
} else {
for (int i = n/2+2; i <= n; i++) printf ("%c",s[i]);
printf ("\n");
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |