BZOJ1396: 识别子串(后缀自动机 线段树)

题意

题目链接

Sol

后缀自动机+线段树

还是考虑通过每个前缀的后缀更新答案,首先出现次数只有一次,说明只有\(right\)集合大小为\(1\)的状态能对答案产生影响

设其结束位置为\(t\),代表的最短/最长后缀的位置为\(l, r\)(l在r的右边)

那么对于区间\(r - l\)内的\(x\)位置,可以用\(t - x+1\)更新答案

对于区间\(l - t\)内的位置,可以用\(l\)更新答案

这两种情况不好一起弄(因为第一种情况肯定要把\(x\)提出来),那么直接开两棵线段树就行了

#include<bits/stdc++.h>
#define chmin(a, b) (a = (a < b ? a : b))
#define chmax(a, b) (a = (a > b ? a : b))
using namespace std;
const int MAXN = 1e5 + 10, INF = 1e9 + 10;
char s[MAXN];
int N, fa[MAXN << 1], len[MAXN << 1], ch[MAXN << 1][26], pos[MAXN << 1], siz[MAXN << 1], las = 1, root = 1, tot = 1;
vector<int> v[MAXN << 1];
void insert(int x, int id) {
    int now = ++tot, pre = las; las = now; pos[now] = id;
    len[now] = len[pre] + 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 ns = ++tot; len[ns] = len[pre] + 1; fa[ns] = fa[q]; 
            memcpy(ch[ns], ch[q], sizeof(ch[q]));
            for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = ns;
            fa[now] = fa[q] = ns;
        }
    }
    siz[now] = 1;
}
void dfs(int x) {
    for(int i = 0; i < v[x].size(); i++) {
        int to = v[x][i];
        dfs(to); siz[x] += siz[to];
    }
}
void Build() {
    for(int i = 2; i <= tot; i++) v[fa[i]].push_back(i);
    dfs(root);
}

struct SegTree {
#define ls k << 1
#define rs k << 1 | 1
    struct Node {
        int l, r, mn, f;
        Node() {
            mn = f = INF;
        }
    }T[MAXN << 2];
    void Build(int k, int ll, int rr) {
        T[k].l = ll; T[k].r = rr; 
        if(ll == rr) return ;
        int mid = ll + rr >> 1;
        Build(ls, ll, mid); Build(rs, mid + 1, rr);
    }
    void ps(int k, int val) {
        chmin(T[k].f, val); 
        chmin(T[k].mn, val);
    }
    void pushdown(int k) {
        if(T[k].f == INF) return ;
        ps(ls, T[k].f); 
        ps(rs, T[k].f);
        T[k].f = INF;
    }
    void IntMin(int k, int ll, int rr, int val) {
        if(ll <= T[k].l && T[k].r <= rr) {ps(k, val); return ;}
        pushdown(k); 
        int mid = T[k].l + T[k].r >> 1;
        if(ll <= mid) IntMin(ls, ll, rr, val);
        if(rr  > mid) IntMin(rs, ll, rr, val);
    }
    int PointQuery(int k, int pos) {
        if(T[k].l == T[k].r) return T[k].mn;
        pushdown(k);
        int mid = T[k].l + T[k].r >> 1;
        if(pos <= mid) return PointQuery(ls, pos);
        if(pos  > mid) return PointQuery(rs, pos);
    }
}T[2];
int main() {
    //freopen("10.in", "r", stdin);freopen("a.out", "w", stdout); 
    scanf("%s", s + 1); N = strlen(s + 1);
    for(int i = 1; i <= N; i++) insert(s[i] - 'a', i);
    T[0].Build(1, 1, N); T[1].Build(1, 1, N);
    Build();
    for(int i = 2; i <= tot; i++) {
        if(siz[i] != 1) continue;
        int l = pos[i] - len[i] + 1, r = pos[i] -  len[fa[i]];
        T[0].IntMin(1, l, r, pos[i] + 1);
        T[1].IntMin(1, r, pos[i], pos[i] - r + 1);
    }
    static int mn[MAXN];
    memset(mn, 0x3f, sizeof(mn));
    for(int i = 1; i <= N; i++) {
        chmin(mn[i], T[0].PointQuery(1, i) - i);
        chmin(mn[i], T[1].PointQuery(1, i));
    }
    for(int i = 1; i <= N; i++) printf("%d\n", mn[i]);
    return 0;
}
/*
aaa
*/
posted @ 2018-12-14 16:50  自为风月马前卒  阅读(348)  评论(0编辑  收藏  举报

Contact with me