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 pll pair<long long, long long>
#define x first
#define y second
using namespace std;
const int N = 2e6 + 5;
pll add(pll a, pll b) { return pll(a.x + b.x, a.y + b.y); }
pll sub(pll a, pll b) { return pll(a.x - b.x, a.y - b.y); }
pll mul(pll a, pll b) { return pll(a.x * b.x, a.y * b.y); }
pll id[N];
int n, cnt, pos;
pll dp[N];
string str;
int main() {
id[1] = {101LL, 103LL};
for(int i = 2;i < N;i++) id[i] = mul(id[i - 1], id[1]);
scanf("%d", &n);
if(!(n & 1)) return !printf("NOT POSSIBLE");
cin >> str;
dp[0] = {str[0], str[0]};
for(int i = 1;i < n;i++) dp[i] = add(mul(pll(str[i], str[i]), id[i]), dp[i - 1]);
if(mul(sub(dp[n >> 1], dp[0]), id[n >> 1]) == sub(dp[n - 1], dp[n >> 1])) cnt++, pos = 0;
if(mul(dp[(n >> 1) - 1], id[n >> 1]) == sub(dp[n - 2], dp[(n >> 1) - 1])) cnt++, pos = n - 1;
for(int i = 1;i < n >> 1;i++) if(sub(dp[n - 1], dp[n >> 1]) == mul(add(sub(dp[n >> 1], dp[i]), mul(dp[i - 1], id[1])), id[n >> 1])) cnt++, pos = i;
if(mul(dp[(n >> 1) - 1], id[(n >> 1) + 1]) == sub(dp[n - 1], dp[n >> 1])) cnt++, pos = n >> 1;
for(int i = (n >> 1) + 1;i < n;i++) if(mul(dp[(n >> 1) - 1], id[(n >> 1) + 1]) == add(mul(sub(dp[i - 1], dp[(n >> 1) - 1]), id[1]), sub(dp[n - 1], dp[i]))) cnt++, pos = i;
if(!cnt) printf("NOT POSSIBLE");
else if(cnt == 1) {
if(!pos) cout << str.substr(1, n >> 1) << endl;
else if(pos == n - 1) cout << str.substr(0, n >> 1) << endl;
else cout << (str.substr(0, pos) + str.substr(pos + 1, n - pos - 1)).substr(0, n >> 1) << endl;
}
else printf("NOT UNIQUE");
return 0;
}
Compilation message (stderr)
friends.cpp: In function 'int main()':
friends.cpp:24:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |