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;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000009
#define LOGN 20
#define MAXN 2000100
const ll A = 9973;
ll powA[MAXN];
ll hashes[MAXN];
ll hash_ss(int l, int r) {
ll ans = (hashes[r] - (hashes[l-1] * powA[r-l + 1] % MOD)) % MOD;
while (ans < 0)
ans += MOD;
return ans;
}
ll hash_concat(int l1, int r1, int l2, int r2) {
ll ans = (hash_ss(l2, r2) + (hash_ss(l1, r1) * powA[r2 - l2 + 1] % MOD)) % MOD;
return ans;
}
int cnt[26];
int main() {
fast
powA[0] = 1;
for (int i = 1; i < MAXN; i++)
powA[i] = (powA[i-1] * A) % MOD;
int N;
string S;
cin >> N >> S;
S = "#" + S;
if (N % 2 == 0) {
cout << "NOT POSSIBLE\n";
return 0;
}
hashes[0] = 0;
for (int i = 1; i <= N; i++)
hashes[i] = (hashes[i-1] * A + (S[i] - 'A' + 1)) % MOD;
for (int i = 1; i <= N; i++)
cnt[S[i] - 'A']++;
int selected_char = -1;
for (int i = 0; i < 26; i++) {
if (cnt[i] % 2 && selected_char != -1) {
cout << "NOT POSSIBLE\n";
return 0;
}
if (cnt[i] % 2)
selected_char = i;
}
int len = N / 2;
vector<pair<int,int>> possible;
for (int i = 1; i <= N; i++) {
if (S[i] - 'A' == selected_char) {
ll left_hash = -1, right_hash = -1;
if (i-1 >= len)
left_hash = hash_ss(1, len);
if (N-i >= len)
right_hash = hash_ss(N-len+1, N);
if (left_hash == -1) {
if (i == 1)
left_hash = hash_ss(2, 1 + len);
else
left_hash = hash_concat(1, i - 1, i + 1, N-len);
}
if (right_hash == -1) {
if (i == N)
right_hash = hash_ss(N-len, N-1);
else
right_hash = hash_concat(len + 1, i - 1, i + 1, N);
}
if (left_hash == right_hash) {
if (i-1 >= len)
possible.push_back({0, len});
else
possible.push_back({1, N-len+1});
}
}
}
if (possible.size() == 0)
cout << "NOT POSSIBLE\n";
else if (possible.size() == 1) {
if (possible[0].f == 0) {
for (int i = 1; i <= possible[0].s; i++)
cout << S[i];
cout << "\n";
} else {
for (int i = possible[0].s; i <= N; i++)
cout << S[i];
cout << "\n";
}
} else
cout << "NOT UNIQUE\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |