神仙题Orz
后缀自动机 + 线段树合并
首先对所有的\(t_i\)建个广义后缀自动机,这样可以得到所有子串信息。
考虑把询问离线,然后把\(S\)拿到自动机上跑,同时维护一下最长能匹配的位置,对于每个以\(i\)位置为右端点的询问我们需要找到\(len\)最小的状态满足\(len[sta] >= pr - pl + 1\)(这部分把每个以\(i\)为端点的询问排序后暴力跳即可,复杂度\(O(n \sqrt{n})\))。那么现在的问题就是对于每个状态,如何知道他在每个\(T_i\)中的出现次数。
直接线段树合并一下就好啦
总复杂度:\(O(大常数的nlogn + 小常数的n\sqrt{n})\)
疯狂wa的原因居然是pair<int, int>不支持自定义小于号
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 10;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, Q;
struct Pair {
int fi, se;
};
bool operator < (const Pair &a, const Pair &b) {
return (a.fi < b.fi) || (a.fi == b.fi && a.se > b.se);
}
Pair operator + (const Pair &a, const Pair &b) {
return {a.fi + b.fi, a.se};
}
Pair ans[MAXN];
char S[MAXN];
string T[MAXN];
struct Query {
int pl, l, r, id;
bool operator < (const Query &rhs) const {
return pl < rhs.pl;
}
};
vector<Query> qry[MAXN];
namespace Seg {
int root[MAXN], ls[MAXN], rs[MAXN], cnt;
Pair mx[MAXN];
void update(int k) {
mx[k] = max(mx[ls[k]], mx[rs[k]]);
}
void IntAdd(int &k, int l, int r, int p, int v) {
if(!k) k = ++cnt;
if(l == r) {mx[k].fi++; mx[k].se = l; return ;}
int mid = l + r >> 1;
if(p <= mid) IntAdd(ls[k], l, mid, p, v);
else IntAdd(rs[k], mid + 1, r, p, v);
update(k);
}
int Merge(int x, int y) {
if(!x || !y) return x ^ y;
int nw = ++cnt; mx[nw] = mx[x];
if(!ls[x] && !rs[x]) {mx[nw].fi += mx[y].fi; return nw;}
ls[nw] = Merge(ls[x], ls[y]);
rs[nw] = Merge(rs[x], rs[y]);
update(nw);
return nw;
}
Pair Query(int k, int l, int r, int ql, int qr) {
if(!k) return {0, 0};
if(ql <= l && r <= qr) return mx[k];
int mid = l + r >> 1;
if(ql > mid) return Query(rs[k], mid + 1, r, ql, qr);
else if(qr <= mid) return Query(ls[k], l, mid, ql, qr);
else {
Pair al = Query(ls[k], l, mid, ql, qr);
Pair ar = Query(rs[k], mid + 1, r, ql, qr);
if((al.fi > ar.fi) || (al.fi == ar.fi && al.se < ar.se)) return al;
else return ar;
}
}
}
namespace SAM {
int fa[MAXN], ch[MAXN][26], len[MAXN], root = 1, las = 1, tot = 1;
vector<int> par[MAXN];
void insert(int x, int id) {
int now = ++tot, pre = las; las = now; len[now] = len[pre] + 1;
if(id) Seg::IntAdd(Seg::root[now], 1, M, id, 1);
for(; pre && !ch[pre][x]; pre = fa[pre]) ch[pre][x] = now;
if(!pre) fa[now] = root;
else {
int q = ch[pre][x];
if(len[pre] + 1 == len[q]) fa[now] = q;
else {
int nq = ++tot; fa[nq] = fa[q]; len[nq] = len[pre] + 1;
memcpy(ch[nq], ch[q], sizeof(ch[q]));
for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nq;
fa[q] = fa[now] = nq;
}
}
}
void Build() {
for(int i = 1; i <= tot; i++) par[fa[i]].push_back(i);
}
void dfs(int x) {
for(auto &to : par[x]) {
dfs(to);
Seg::root[x] = Seg::Merge(Seg::root[x], Seg::root[to]);
}
}
void work() {
int now = root, dl = 0;
for(int i = 1; i <= N; i++) {
int nxt = S[i] - 'a';
while(!ch[now][nxt] && now) now = fa[now], dl = len[now];
if(!now) {
now = 1; dl = 0;
for(auto &q : qry[i]) ans[q.id].se = q.l;
continue;
}
now = ch[now][nxt]; dl++;
int t = now;
for(auto &q : qry[i]) {
if(dl < i - q.pl + 1) {ans[q.id].se = q.l; continue;}
while(len[fa[t]] >= i - q.pl + 1) t = fa[t];
ans[q.id] = Seg::Query(Seg::root[t], 1, M, q.l, q.r);
if(!ans[q.id].fi) ans[q.id].se = q.l;
}
}
}
}
int main() {
// freopen("a.in", "r", stdin);
scanf("%s", S + 1);
N = strlen(S + 1);
cin >> M;
for(int i = 1; i <= M; i++) {
cin >> T[i];
string &ns = T[i];
for(int j = 0; j < ns.length(); j++) SAM::insert(ns[j] - 'a', i);
SAM::las = 1;
}
SAM::Build();
SAM::dfs(1);
// for(int i = 1; i <= Seg::cnt; i++) printf("%d ", Seg::mx[i]);
cin >> Q;
for(int i = 1; i <= Q; i++) {
int l = read(), r = read(), pl = read(), pr = read();
qry[pr].push_back({pl, l, r, i});
}
for(int i = 1; i <= N; i++) stable_sort(qry[i].begin(), qry[i].end());
SAM::work();
for(int i = 1; i <= Q; i++) {
printf("%d %d\n", ans[i].se, ans[i].fi);
}
return 0;
}