BZOJ4698: Sdoi2008 Sandy的卡片(字尾陣列 二分)

自為風月馬前卒發表於2018-11-28

題意

題目連結

Sol

不要問我為什麼發兩篇blog,就是為了騙訪問量

字尾陣列的也比較好想,先把所有位置差分,然後在height陣列中二分就行了

資料好水啊

// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 10;
const int INF = 2333;
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 T, N, M, a[MAXN], id[MAXN], rak[MAXN], tp[MAXN], tax[MAXN], H[MAXN], vis[MAXN], sa[MAXN], ti, mx;
void Qsort() {
    for(int i = 0; i <= M; i++) tax[i] = 0;
    for(int i = 1; i <= N; i++) tax[rak[i]]++;
    for(int i = 1; i <= M; i++) tax[i] += tax[i - 1];
    for(int i = N; i >= 1; i--) sa[tax[rak[tp[i]]]--] = tp[i];
}
void SuffixSort() {
    for(int i = 1; i <= N; i++) rak[i] = a[i], tp[i] = i; 
    M = N + INF; Qsort();
    for(int w = 1, p = 0; p < N; w <<= 1, M = p) { p = 0;
        for(int i = 1; i <= w; i++) tp[++p] = N - i + 1;
        for(int i = 1; i <= N; i++) if(sa[i] > w) tp[++p] = sa[i] - w;
        Qsort(); swap(rak, tp); rak[sa[1]] = p = 1;
        for(int i = 2; i <= N; i++) rak[sa[i]] = (tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + w] == tp[sa[i - 1] + w]) ? p : ++p; 
    }
    for(int i = 1, k = 0; i <= N; i++) {
        if(k) k--; int j = sa[rak[i] - 1];
        while(a[i + k] == a[j + k]) k++;
        H[rak[i]] = k;
    }
}
bool check(int len) {
    ti++; int now = 0;
    for(int i = 1; i <= N; i++) {
        if(H[i] < len) {now = 0, ti++; continue;}
        if(vis[id[sa[i]]] != ti) vis[id[sa[i]]] = ti, now++; 
        if(now == T) return 1;
    }
    return 0;
}
int main() {
    //freopen("a.in", "r", stdin);
    T = read();
    for(int i = 1; i <= T; i++) {
        int num = read() - 1, pre = read(); a[++N] = i; id[N] = i; mx = max(mx, num + 1);
        while(num--) a[++N] = read() - pre + INF, pre = a[N] + pre - INF, id[N] = i;
    }
    SuffixSort();
    int l = 1, r = mx, ans = 0;
    while(l <= r) {
        int mid = l + r >> 1;
        if(check(mid)) ans = mid, l = mid + 1;
        else r = mid - 1;
    }
    printf("%d", ans + 1);
    return 0;
}

相關文章