이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const string not_possible = "NOT POSSIBLE";
const string not_unique = "NOT UNIQUE";
const ll MOD = 1645333507;
const ll BASE = 37;
int n, m;
string s;
ll mod(ll x) {
x %= MOD;
if (x < 0)
return x + MOD;
else
return x;
}
ll append(ll prev, char a) { return mod(prev * BASE + ll(a - 'A')); }
ll fastpow(ll x, ll pot) {
ll result = 1;
ll current = x;
while (pot != 0) {
if (pot & 1) result = mod(result * current);
current = mod(current * current);
pot >>= 1;
}
return result;
}
ll divide(ll x, ll y) { return mod(x * fastpow(y, MOD - 2)); }
vector<int> h;
void buildHash() {
h.resize(n);
for (int i = 0; i < n; i++) {
h[i] = append(i > 0 ? h[i - 1] : 0, s[i]);
}
}
ll getHash(int i, int j) {
ll result = mod(h[j] - (i > 0 ? h[i - 1] : 0) * fastpow(BASE, j - i + 1));
return result;
}
ll getHash(int a, int b, int c, int d) {
ll result = mod(getHash(a, b) * fastpow(BASE, d - c + 1) + getHash(c, d));
return result;
}
int main() {
cin >> n >> s;
m = n / 2;
buildHash();
vector<int> ans;
if (getHash(1, m) == getHash(m + 1, n - 1)) ans.push_back(0);
if (getHash(0, m - 1) == getHash(m, n - 2)) ans.push_back(n - 1);
if (getHash(0, m - 1) == getHash(m + 1, n - 1)) ans.push_back(m);
for (int i = 1; i < m; i++) {
if (getHash(0, i - 1, i + 1, m) == getHash(m + 1, n - 1)) ans.push_back(i);
}
for (int i = m + 1; i < n - 1; i++) {
if (getHash(0, m - 1) == getHash(m, i - 1, i + 1, n - 1)) ans.push_back(i);
}
if (ans.size() == 0)
cout << not_possible << endl;
else if (ans.size() > 1)
cout << not_unique << endl;
else
cout << s.substr(ans[0] < m ? m + 1 : 0, m) << endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |