이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
int len = N / 2;
bool left = 0, right = 0;
ll leftmostH = hash_ss(1, len);
ll rightmostH = hash_ss(N-len+1, N);
if (leftmostH == rightmostH) {
for (int i = 1; i <= len; i++)
cout << S[i];
cout << "\n";
return 0;
}
ll left_hash, right_hash;
for (int i = 1; i <= N/2; i++) {
if (i == 1)
left_hash = hash_ss(2, N/2+1);
else
left_hash = hash_concat(1, i - 1, i + 1, N/2+1);
if (left_hash == rightmostH)
right = true;
}
for (int i = N/2+1; i <= N; i++) {
if (i == N)
right_hash = hash_ss(N/2+1, N-1);
else
right_hash = hash_concat(N/2+1, i - 1, i + 1, N);
if (right_hash == leftmostH)
left = true;
}
if (!left && !right)
cout << "NOT POSSIBLE\n";
else if (left && !right) {
for (int i = 1; i <= len; i++)
cout << S[i];
cout << "\n";
} else if (right && !left) {
for (int i = N-len+1; i <= N; i++)
cout << S[i];
cout << "\n";
} else if (left && right)
cout << "NOT UNIQUE\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |