BZOJ4698: Sdoi2008 Sandy的卡片(后缀数组 二分)

题意

题目链接

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;
}
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2018-11-28 14:59  自为风月马前卒  阅读(223)  评论(0编辑  收藏  举报

Contact with me