Bzoj4556: [Tjoi2016&Heoi2016]字符串
题面
Sol
二分这个最长前缀的长度
考虑\(check\)
首先\(s[a..b]\)以\(i\)开头子串如果要满足和\(s[c..d]\)的\(LCP>=mid\)
那么\(i\)肯定是在后缀数组的\(rank\)的一个区间内
这个区间显然可以二分/倍增出来
\(i\)同时还要满足\(b-i+1>=mid\)和\(i>=a\),这也是一个区间
那么变成了一个二维数点问题
直接主席树搞一搞就好了
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
template <class Int>
IL void Input(RG Int &x){
RG int z = 1; RG char c = getchar(); x = 0;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= z;
}
const int maxn(1e5 + 5);
const int oo(1e9);
int n, sa[maxn], rk[maxn], tmp[maxn], t[maxn], st[20][maxn], lg[maxn], q;
char s[maxn];
IL int Cmp(RG int i, RG int j, RG int k){
return tmp[i] == tmp[j] && tmp[i + k] == tmp[j + k];
}
IL void SuffixSort(){
RG int m = 30;
for(RG int i = 1; i <= n; ++i) ++t[rk[i] = s[i] - 'a' + 1];
for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
for(RG int i = n; i; --i) sa[t[rk[i]]--] = i;
for(RG int k = 1; k <= n; k <<= 1){
RG int l = 0;
for(RG int i = n - k + 1; i <= n; ++i) tmp[++l] = i;
for(RG int i = 1; i <= n; ++i) if(sa[i] > k) tmp[++l] = sa[i] - k;
for(RG int i = 0; i <= m; ++i) t[i] = 0;
for(RG int i = 1; i <= n; ++i) ++t[rk[tmp[i]]];
for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
for(RG int i = n; i; --i) sa[t[rk[tmp[i]]]--] = tmp[i];
swap(rk, tmp), rk[sa[1]] = l = 1;
for(RG int i = 2; i <= n; ++i) rk[sa[i]] = Cmp(sa[i - 1], sa[i], k) ? l : ++l;
if(l >= n) break;
m = l;
}
for(RG int i = 1, h = 0; i <= n; ++i){
if(h) --h;
while(s[i + h] == s[sa[rk[i] - 1] + h]) ++h;
st[0][rk[i]] = h;
}
for(RG int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
for(RG int j = 1; j <= lg[n]; ++j)
for(RG int i = 1; i + (1 << j) - 1 <= n; ++i)
st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}
IL int LCP(RG int x, RG int y){
if(x == y) return oo;
if(x > y) swap(x, y);
RG int l = lg[y - x];
return min(st[l][x + 1], st[l][y - (1 << l) + 1]);
}
int tot, rt[maxn];
struct HJT{
int ls, rs, sum;
} tree[maxn * 20];
IL void Modify(RG int &x, RG int l, RG int r, RG int p){
tree[++tot] = tree[x], ++tree[x = tot].sum;
if(l == r) return;
RG int mid = (l + r) >> 1;
if(p <= mid) Modify(tree[x].ls, l, mid, p);
else Modify(tree[x].rs, mid + 1, r, p);
}
IL int Query(RG int x, RG int y, RG int l, RG int r, RG int ql, RG int qr){
if(!(x + y)) return 0;
if(ql <= l && qr >= r) return tree[x].sum - tree[y].sum;
RG int mid = (l + r) >> 1, ans = 0;
if(ql <= mid) ans = Query(tree[x].ls, tree[y].ls, l, mid, ql, qr);
if(qr > mid) ans += Query(tree[x].rs, tree[y].rs, mid + 1, r, ql, qr);
return ans;
}
IL int Check(RG int l1, RG int r1, RG int p, RG int len){
RG int l = 1, r = p, ql, qr;
while(l <= r){
RG int mid = (l + r) >> 1;
if(LCP(mid, p) >= len) ql = mid, r = mid - 1;
else l = mid + 1;
}
l = p, r = n;
while(l <= r){
RG int mid = (l + r) >> 1;
if(LCP(mid, p) >= len) qr = mid, l = mid + 1;
else r = mid - 1;
}
return Query(rt[qr], rt[ql - 1], 1, n, l1, r1);
}
int main(RG int argc, RG char* argv[]){
Input(n), Input(q), scanf(" %s", s + 1);
SuffixSort();
for(RG int i = 1; i <= n; ++i) rt[i] = rt[i - 1], Modify(rt[i], 1, n, sa[i]);
for(RG int i = 1, a, b, c, d; i <= q; ++i){
Input(a), Input(b), Input(c), Input(d);
RG int l = 0, r = min(b - a + 1, d - c + 1), ans = 0;
while(l <= r){
RG int mid = (l + r) >> 1;
if(Check(a, b - mid + 1, rk[c], mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%d\n", ans);
}
return 0;
}