# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
844953 | vjudge1 | Three Friends (BOI14_friends) | C++17 | 73 ms | 42832 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>
#define ll long long
#define ld long double
#define int long long
using namespace std;
const int N = 2 << 20;
const int INF = INT_MAX;
const int mod = 1e9+7;
const int base = 101;
int n,pred[N];
string s,ans;
int half,lab[N];
int get(int u, int v) {
return (pred[v] - pred[u-1]*lab[v-u+1] + mod*mod)%mod;
}
bool check(int u, int v) {
int mid = u + v >> 1;
return (get(u,mid) == get(mid+1,v));
}
signed main()
{
if (fopen("solve.inp", "r")) {
freopen("solve.inp", "r", stdin);
freopen("solve.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
ans = "";
if(n % 2 == 0) {
cout << "NOT POSSIBLE";
return 0;
}
s = " " + s;
lab[0] = 1;
for(int i = 1 ; i <= n ; i++){
lab[i] = lab[i-1]*base%mod;
}
for(int i = 1 ; i <= n ; i++) {
pred[i] = (pred[i-1]*base+s[i])%mod;
}
half = n/2;
if(check(2,n)) {
for(int i = 2 ; i <= n ; i++) {
ans += s[i];
}
}
if(check(1,n-1)) {
if(ans != "") {
cout << "NOT UNIQUE";
return 0;
}
for(int i = 1 ; i < n ; i++) {
ans += s[i];
}
}
for(int i = 2, mid ; i < n ; i++) {
mid = half;
int a = 0;
int b = 0;
if(i <= half) {
a = get(1,i-1);
mid -= i-1;
a = (a*lab[mid] + get(i+1,i+mid))%mod;
b = get(i+mid+1,n);
}
else {
a = get(1,half);
b = get(half+1,i-1);
mid -= i-half-1;
b = (b*lab[n-i]+get(i+1,n))%mod;
}
if(a == b) {
if(ans != "") {
cout << "NOT UNIQUE";
return 0;
}
for(int j = 1 ; j <= n ; j++) {
if(j == i) {
continue;
}
ans += s[j];
}
}
}
if(ans == "") {
cout << "NOT POSSIBLE";
}
else {
for(int i = 0 ; i < half ; i++) {
cout << ans[i];
}
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |