QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#48021 | #243. Lyndon Substring | hos_lyric | AC ✓ | 460ms | 59848kb | C++14 | 11.0kb | 2022-09-11 08:30:29 | 2022-09-11 08:30:31 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
////////////////////////////////////////////////////////////////////////////////
// SA-IS
// String: string, vector<int>, vector<long long>
// if sigma <= n, O(n) time, O(n) space
// if sigma > n, O(n log n) time, O(n) space
template <class String> vector<int> suffixArrayRec(const String &as) {
const int n = as.size();
if (n == 0) return {};
const auto minmaxA = minmax_element(as.begin(), as.end());
const auto minA = *minmaxA.first, maxA = *minmaxA.second;
if (static_cast<unsigned long long>(maxA) - minA >=
static_cast<unsigned long long>(n)) {
vector<int> us(n);
for (int u = 0; u < n; ++u) us[u] = u;
std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {
return (as[u] < as[v]);
});
int b = 0;
vector<int> bs(n, 0);
for (int i = 1; i < n; ++i) {
if (as[us[i - 1]] != as[us[i]]) ++b;
bs[us[i]] = b;
}
return suffixArrayRec(bs);
}
const int sigma = maxA - minA + 1;
vector<int> pt(sigma + 1, 0), poss(sigma);
for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1];
for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a];
// cmp[u] := (as[u, n) < as[u + 1, n))
vector<bool> cmp(n);
cmp[n - 1] = false;
for (int u = n - 1; --u >= 0; ) {
cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1];
}
// ><, nn - id (0 <= id < n)
int nn = 0;
vector<int> ids(n, 0);
int last = n;
vector<int> nxt(n, 0);
// put ><, from the tail of each bucket
vector<int> us(n, 0);
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int u = n - 1; --u >= 1; ) if (!cmp[u - 1] && cmp[u]) {
ids[u] = ++nn;
nxt[u] = last;
last = u;
us[--poss[as[u] - minA]] = u;
}
// follow > backwards, from the head of each bucket
memcpy(poss.data(), pt.data(), sigma * sizeof(int));
us[poss[as[n - 1] - minA]++] = n - 1;
for (int i = 0; i < n; ++i) {
const int u = us[i];
if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
}
// follow < backwards, from the tail of each bucket
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int i = n; --i >= 0; ) {
const int u = us[i];
if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
}
if (nn) {
int vsLen = 0;
vector<int> vs(nn);
for (const int u : us) if (ids[u]) vs[vsLen++] = u;
int b = 0;
vector<int> bs(nn, 0);
for (int j = 1; j < nn; ++j) {
// as[v1, w1] (< or =) as[v0, w0]
int v1 = vs[j - 1], v0 = vs[j];
const int w1 = nxt[v1], w0 = nxt[v0];
if (w1 - v1 != w0 - v0) {
++b;
} else {
for (; ; ++v1, ++v0) {
if (v1 == n) { ++b; break; }
if (as[v1] != as[v0]) { ++b; break; }
if (v1 == w1) break;
}
}
bs[nn - ids[vs[j]]] = b;
}
for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u;
const auto sub = suffixArrayRec(bs);
// put ><, from the tail of each bucket
memset(us.data(), 0, n * sizeof(int));
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int j = nn; --j >= 0; ) {
const int u = vs[sub[j]];
us[--poss[as[u] - minA]] = u;
}
// follow > backwards, from the head of each bucket
memcpy(poss.data(), pt.data(), sigma * sizeof(int));
us[poss[as[n - 1] - minA]++] = n - 1;
for (int i = 0; i < n; ++i) {
const int u = us[i];
if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
}
// follow < backwards, from the tail of each bucket
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int i = n; --i >= 0; ) {
const int u = us[i];
if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
}
}
return us;
}
// us[i]: i-th suffix
// su[u]: index of as[u, n)
// hs[i]: longest common prefix of as[us[i - 1], n) and as[us[i], n)
struct SuffixArray {
int n;
bool rmq;
vector<int> us, su, hs;
SuffixArray() : n(0), rmq(false) {}
SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); }
SuffixArray(const vector<int> &as, bool rmq_) : rmq(rmq_) { build(as); }
SuffixArray(const vector<long long> &as, bool rmq_) : rmq(rmq_) { build(as); }
template <class String> void build(const String &as) {
n = as.size();
us = suffixArrayRec(as);
su.resize(n + 1);
for (int i = 0; i < n; ++i) su[us[i]] = i;
su[n] = -1;
hs.assign(n, 0);
for (int h = 0, u = 0; u < n; ++u) if (su[u]) {
for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {}
hs[su[u]] = h;
if (h) --h;
}
if (rmq) {
const int logN = n ? (31 - __builtin_clz(n)) : 0;
hs.resize((logN + 1) * n - (1 << logN) + 1);
for (int e = 0; e < logN; ++e) {
int *hes = hs.data() + e * n;
for (int i = 0; i <= n - (1 << (e + 1)); ++i) {
hes[n + i] = min(hes[i], hes[i + (1 << e)]);
}
}
}
}
// Returns longest common prefix of as[u, n) and as[v, n).
// 0 <= u, v <= n
// Assumes rmq.
inline int lcp(int u, int v) const {
if (u == v) return n - u;
int i = su[u], j = su[v];
if (i > j) swap(i, j);
const int e = 31 - __builtin_clz(j - i);
return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]);
}
};
////////////////////////////////////////////////////////////////////////////////
template <class String>
vector<pair<int, int>> lyndonSuffix(const String &as) {
const int n = as.size();
vector<pair<int, int>> pqs(n + 1);
pqs[0] = make_pair(0, 0);
for (int u = 0; u < n; ) {
for (int p = 1, q = 1, r = 0, v = u + 1; ; ++v) {
// as[u, v) = as[u, u + p)^q as[u, u + r)
// as[u, u + p): Lyndon
pqs[v] = (r != 0) ? pqs[u + r] : make_pair(p, q);
if (v == n || as[v - p] > as[v]) {
u = v - r;
break;
} else if (as[v - p] < as[v]) {
p = v + 1 - u; q = 1; r = 0;
} else {
if (++r == p) { ++q; r = 0; }
}
}
}
return pqs;
}
char buf[100'010];
int N, Q;
vector<string> S;
int main() {
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%d%d", &N, &Q);
S.resize(N);
for (int i = 0; i < N; ++i) {
scanf("%s", buf);
S[i] = buf;
}
vector<int> L(N);
string cat;
vector<int> head(N);
for (int i = 0; i < N; ++i) {
L[i] = S[i].size();
head[i] = cat.size();
cat += S[i];
cat += '!';
}
const SuffixArray sa(cat, true);
vector<int> inside(N, 0);
vector<vector<int>> cuts(N);
vector<vector<int>> uss(N);
for (int i = 0; i < N; ++i) {
const auto pqs = lyndonSuffix(S[i]);
for (int v = L[i]; ; ) {
cuts[i].push_back(v);
if (v == 0) break;
const int p = pqs[v].first;
chmax(inside[i], p);
v -= p;
}
reverse(cuts[i].begin(), cuts[i].end());
for (int v = L[i]; ; ) {
uss[i].push_back(v);
if (v == 0) break;
const int p = pqs[v].first;
const int q = pqs[v].second;
const int u = v - q * p;
if (!(p >= L[i] - v && sa.lcp(head[i] + u, head[i] + v) >= L[i] - v)) break;
v = u;
}
reverse(uss[i].begin(), uss[i].end());
}
// cerr<<"inside = "<<inside<<endl;
// cerr<<"cuts = "<<cuts<<endl;
// cerr<<"uss = "<<uss<<endl;
for (; Q--; ) {
int i, j;
scanf("%d%d", &i, &j);
--i;
--j;
int ans = max(inside[i], inside[j]);
// min_u S[i][u, L[i]) S[j]
int um = -1;
for (const int u : uss[i]) {
// S[i][um, L[i]) S[j] vs S[i][u, L[i]) (um < u)
if (!~um || [&]() -> bool {
// S[i][um, *) vs S[i][u, L[i])
{
const int res = sa.lcp(head[i] + um, head[i] + u);
if (res < L[i] - u) return (S[i][um + res] > S[i][u + res]);
}
// S[i][*, L[i]) vs S[j][0, *)
{
const int res = sa.lcp(head[i] + um + (L[i] - u), head[j]);
if (res < min(u - um, L[j])) return (S[i][um + (L[i] - u) + res] > S[j][res]);
}
if (u - um > L[j]) return true;
// S[j] vs S[j][*, L[j])
{
const int res = sa.lcp(head[j], head[j] + (u - um));
if (res < L[j] - (u - um)) return (S[j][res] > S[j][u - um + res]);
}
return true;
}()) {
um = u;
}
}
// cerr<<i<<" "<<j<<": um = "<<um<<endl;
int lo = -1, hi = (int)cuts[j].size() - 1;
for (; lo + 1 < hi; ) {
const int mid = (lo + hi) / 2;
const int pos = cuts[j][mid];
// S[i][um, L[i]) S[j] vs S[j][pos, L[j])
if ([&]() -> bool {
// S[i][um, L[i]) vs S[j][pos, *)
{
const int res = sa.lcp(head[i] + um, head[j] + pos);
if (res < min(L[i] - um, L[j] - pos)) return (S[i][um + res] > S[j][pos + res]);
}
if (L[i] - um > L[j] - pos) return true;
// S[j] vs S[j][*, L[j])
{
const int res = sa.lcp(head[j], head[j] + pos + (L[i] - um));
if (res < L[j] - pos - (L[i] - um)) return (S[j][res] > S[j][pos + (L[i] - um) + res]);
}
return true;
}()) {
// cerr<<" "<<(S[i].substr(um)+"|"+S[j])<<" > "<<S[j].substr(pos)<<endl;
hi = mid;
} else {
// cerr<<" "<<(S[i].substr(um)+"|"+S[j])<<" < "<<S[j].substr(pos)<<endl;
lo = mid;
}
}
chmax(ans, (L[i] - um) + cuts[j][hi]);
printf("%d\n", ans);
#ifdef LOCAL
const auto pqs=lyndonSuffix(S[i]+S[j]);
cerr<<i<<" "<<j<<": "<<pqs<<endl;
int brt=0;
for(int v=L[i]+L[j];v>0;){const int p=pqs[v].first;chmax(brt,p);v-=p;}
cerr<<"brt = "<<brt<<endl;
assert(brt==ans);
#endif
}
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 460ms
memory: 59848kb
input:
24 100 10000 ab babbab baabbbb abbababbb bb bbbaaaba b aababaa aaabbb abbabbaaa aaaab bbab bbabb baaa bbabbaaba abaa b aabbbb babbbbbb bbaaba aba bbb abbbaabaa aa bbaabaabbb abbbbbb aabbbaabba ababba aabbaab ababa aabbabbaab b aabaabaab baaababa babaaa abaaaabbbb aaaaabaa bbb bbab baaa bbabba ababbb...
output:
2 3 6 11 4 5 3 5 6 8 5 4 4 3 4 2 3 6 10 4 2 5 6 2 8 9 5 7 4 2 7 3 3 6 3 8 6 5 4 3 4 8 3 4 7 2 2 3 9 7 2 2 7 11 3 5 5 8 3 6 6 3 5 4 4 8 4 2 5 8 3 5 5 4 5 3 2 5 3 10 4 5 5 9 4 4 7 3 5 3 5 9 4 4 3 7 8 6 5 3 3 3 6 11 7 8 3 5 6 8 5 7 7 3 7 3 3 6 13 7 3 8 6 3 8 9 5 7 4 3 7 3 3 6 3 8 6 8 7 3 7 8 3 4 10 3 3...
result:
ok 600000 lines