[SPOJ8222]NSUBSTR - Substrings

NSUBSTR - Substrings

You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as the maximal number of times that some string with length x appears in S. For example for string ‘ababa’ F(3) will be 2 because there is a string ‘aba’ that occurs twice. Your task is to output F(i) for every i so that 1<=i<=|S|.
Input
String S consists of at most 250000 lowercase latin letters.
Output
Output |S| lines. On the i-th line output F(i).
Example
Input:
ababa
Output:
3
2
2
1
1

Solution
关键问题在于统计某个串出现了多少次。
在后缀自动机中,答案即为包含了这个串的状态的right集合的大小。
后缀自动机有两张DAG,一张是trans图,一张是parent树
从trans图的角度出发,right集合的大小为该状态走到结束状态的方案数
从parent树的角度出发,parent树是反串的后缀树,right集合的大小为该状态的子树中有多少个结点代表了反串的一个后缀(也就是原串的前缀)
我们采取第二种计数方法,先将包含原串前缀的状态的right集合设为1(相当于在反串的后缀树中将后缀结点标记为1)
因为parent树中我们并没有把儿子记下来,所以没办法直接对parent树bfs,但我们知道儿子的len严格大于父亲的len,所以我们按len的长度进行排序,然后按len从大到小,用当前点更新parent的答案
我们发现一个结点代表的长度是一个区间,我们先只考虑该结点代表的最长长度,然后我们再用长串去更新短串(因为一个长串出现k次,它的所有后缀都至少出现k次)即可

Code

#include <bits/stdc++.h>
using namespace std;

#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
inline void ckmax(int &x, int y) { if (x < y) x = y; }
inline void ckmin(int &x, int y) { if (x > y) x = y; }

const int N = 500100;
char s[N];
int root, last, cnt = 0, len;
int l[N], nxt[N][26], fa[N], r[N];
int d[N], t[N], f[N];

inline void insert(int c){
    int np = ++cnt, p = last; last = np;
    l[np] = l[p]+1;
    for (; p && !nxt[p][c]; nxt[p][c] = np, p = fa[p]);
    if (!p) fa[np] = root;
    else if (l[nxt[p][c]] == l[p]+1) fa[np] = nxt[p][c];
    else{
        int nq = ++cnt, q = nxt[p][c]; l[nq] = l[p]+1;
        fa[nq] = fa[q]; memcpy(nxt[nq], nxt[q], sizeof nxt[q]);
        fa[np] = fa[q] = nq;
        for (; p && nxt[p][c] == q; nxt[p][c] = nq, p = fa[p]);
    }
}
inline void topsort(){
    rep(i, 1, cnt) d[l[i]]++;
    rep(i, 1, len) d[i] += d[i-1];
    rep(i, 1, cnt) t[d[l[i]]--] = i;
}
int main(){
    scanf("%s", s+1); len = strlen(s+1);
    root = last = ++cnt;
    rep(i, 1, len) insert(s[i]-'a');
    for (int i = 1, p = root; i <= len; i++) 
        p = nxt[p][s[i]-'a'], r[p]++;
    topsort();
    per(i, cnt, 1) r[fa[t[i]]] += r[t[i]];
    rep(i, 1, cnt) ckmax(f[l[i]], r[i]);
    per(i, len, 1) ckmax(f[i], f[i+1]);
    rep(i, 1, len) printf("%d\n", f[i]);
    return 0;
}

你可能感兴趣的:(后缀自动机)