이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
namespace std {
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
out << "[";
for (int i = 0; i < (int)vec.size(); ++i) {
out << vec[i];
if (i + 1 < (int)vec.size())
out << ", ";
}
return out << "]";
}
} // namespace std
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << H;
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
/*
* sa[i] = starting index of i-th smallest suffix
* sa.len() = n+ 1, sa[0] = n
* lcp[i] = lcp(sa[i], sa[i-1]), lcp[0] = 0, len(sa) = n+1
*/
template <class T> struct RMQ {
vector<vector<T>> jmp;
RMQ(const vector<T> &V) : jmp(1, V) {
for (int pw = 1, k = 1; pw * 2 <= (int)V.size(); pw *= 2, ++k) {
jmp.emplace_back((int)V.size() - pw * 2 + 1);
for (int j = 0; j < (int)jmp[k].size(); ++j)
jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]);
}
}
T query(int a, int b) { // [a, b)
assert(a < b);
int dep = 31 - __builtin_clz(b - a);
return min(jmp[dep][a], jmp[dep][b - (1 << dep)]);
}
};
vector<int> sa_is(const vector<int> &s, int lim = 256) {
int n = s.size();
if (!n)
return {};
vector<int> sa(n);
vector<bool> ls(n);
for (int i = n - 2; i >= 0; --i)
ls[i] = s[i] == s[i + 1] ? ls[i + 1] : s[i] < s[i + 1];
vector<int> sum_l(lim), sum_s(lim);
for (int i = 0; i < n; ++i)
(ls[i] ? sum_l[s[i] + 1] : sum_s[s[i]])++;
for (int i = 0; i < lim; ++i) {
if (i)
sum_l[i] += sum_s[i - 1];
sum_s[i] += sum_l[i];
}
auto induce = [&](const vector<int> &lms) {
fill(sa.begin(), sa.end(), -1);
vector<int> buf = sum_s;
for (int d : lms)
if (d != n)
sa[buf[s[d]]++] = d;
buf = sum_l;
sa[buf[s[n - 1]]++] = n - 1;
for (int i = 0; i < n; ++i) {
int v = sa[i] - 1;
if (v >= 0 && !ls[v])
sa[buf[s[v]]++] = v;
}
buf = sum_l;
for (int i = n - 1; i >= 0; --i) {
int v = sa[i] - 1;
if (v >= 0 && ls[v])
sa[--buf[s[v] + 1]] = v;
}
};
vector<int> lms_map(n + 1, -1), lms;
int m = 0;
for (int i = 1; i < n; ++i)
if (!ls[i - 1] && ls[i])
lms_map[i] = m++, lms.push_back(i);
induce(lms);
vector<int> sorted_lms;
for (auto &v : sa)
if (lms_map[v] != -1)
sorted_lms.push_back(v);
vector<int> rec_s(m);
int rec_upper = 0;
for (int i = 1; i < m; ++i) {
int l = sorted_lms[i - 1], r = sorted_lms[i];
int end_l = lms_map[l] + 1 < m ? lms[lms_map[l] + 1] : n;
int end_r = lms_map[r] + 1 < m ? lms[lms_map[r] + 1] : n;
bool same = 0;
if (end_l - l == end_r - r) {
for (; l < end_l && s[l] == s[r]; ++l, ++r)
;
if (l != n && s[l] == s[r])
same = 1;
}
rec_s[lms_map[sorted_lms[i]]] = (rec_upper += !same);
}
vector<int> rec_sa = sa_is(rec_s, rec_upper + 1);
for (int i = 0; i < m; ++i)
sorted_lms[i] = lms[rec_sa[i]];
induce(sorted_lms);
return sa;
}
struct SuffixArray {
vector<int> sa, lcp, rank;
string s;
RMQ<int> rmq;
SuffixArray(string &_s, int lim = 256) : rmq({}) {
s = _s;
vector<int> S;
for (char c : _s)
S.push_back(c);
int n = s.size() + 1, k = 0, a, b;
auto SA = sa_is(S, lim);
vector<int> y(n);
sa = lcp = rank = y;
sa[0] = n - 1;
for (int i = 1; i < n; ++i)
sa[i] = SA[i - 1];
for (int i = 1; i < n; ++i)
rank[sa[i]] = i;
for (int i = 0, j; i < n - 1; lcp[rank[i++]] = k)
for (k &&k--, j = sa[rank[i] - 1]; s[i + k] == s[j + k]; k++)
;
rmq = RMQ<int>(lcp);
}
int getLCP(int i, int j) {
if (i == j)
return s.size() - i;
i = rank[i];
j = rank[j];
if (i > j)
swap(i, j);
return rmq.query(i + 1, j + 1);
}
};
bool get(string U) {
int N = U.size() / 2;
SuffixArray suffix(U);
for (int skip = N; skip < 2 * N + 1; ++skip) {
if (suffix.getLCP(0, N) < skip - N)
continue;
if (skip < 2 * N and suffix.getLCP(skip - N, skip + 1) < N - (skip - N))
continue;
return true;
}
return false;
}
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N;
string U;
cin >> N >> U;
if (N % 2 == 0) {
cout << "NOT POSSIBLE\n";
return 0;
}
string T = U;
reverse(T.begin(), T.end());
bool okL = get(U);
bool okR = get(T);
N /= 2;
string L = U.substr(0, N);
string R = U.substr(N + 1, N);
dbg(L, R);
dbg(okL, okR);
if (!okL and !okR) {
cout << "NOT POSSIBLE\n";
return 0;
}
if (okL and okR and L != R) {
cout << "NOT UNIQUE\n";
return 0;
}
cout << (okL ? L : R) << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
friends.cpp: In constructor 'SuffixArray::SuffixArray(std::string&, long long int)':
friends.cpp:129:34: warning: unused variable 'a' [-Wunused-variable]
129 | int n = s.size() + 1, k = 0, a, b;
| ^
friends.cpp:129:37: warning: unused variable 'b' [-Wunused-variable]
129 | int n = s.size() + 1, k = 0, a, b;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |