前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SPOJ8222 NSUBSTR - Substrings(后缀自动机)

SPOJ8222 NSUBSTR - Substrings(后缀自动机)

作者头像
attack
发布2018-07-04 15:09:00
2250
发布2018-07-04 15:09:00
举报

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

代码语言:javascript
复制
Input:
ababa

Output:
3
2
2
1
1

 Submit solution!

为什么我的后缀自动机写的这么奇怪qwq。。

为什么泥萌都在写桶排??!!

这题不是dfs一下求出$right$集合来就完了么qwq、、

代码语言:javascript
复制
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = 250001 << 1, INF = 1e9 + 10;
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;
}
char s[MAXN];
int N;
int f[MAXN], siz[MAXN], fa[MAXN], len[MAXN], ch[MAXN][26], root = 1, last = 1, tot = 1;
void insert(int x) {
    int now = ++tot, pre = last; last = now;
    siz[now] = 1;
    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[q] == len[pre] + 1) fa[now] = q;
        else {
            int nows = ++tot; 
            memcpy(ch[nows], ch[q], sizeof(ch[q]));
            fa[nows] = fa[q]; fa[q] = fa[now] = nows;
            len[nows] = len[pre] + 1;
            for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nows;
        }
    }
}
vector<int> v[MAXN];
void dfs(int x) {
    for(int i = 0; i < v[x].size(); i++) {
        dfs(v[x][i]);
        siz[x] += siz[v[x][i]];
    }
    f[len[x]] = max(f[len[x]], siz[x]);
}
int main() {
#ifdef WIN32
    freopen("a.in", "r", stdin);
#else
    //freopen("pow.in", "r", stdin);
    //freopen("pow.out", "w", stdout); 
#endif    
    scanf("%s", s + 1);
    N = strlen(s + 1);
    for(int i = 1; i <= N; i++) insert(s[i] - 'a');
    for(int i = 2; i <= tot; i++) v[fa[i]].push_back(i);
    dfs(root);
    for(int i = 1; i <= N; i++) 
        printf("%d\n", f[i]);
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-06-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input
  • Output
  • Example
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档