码迷,mamicode.com
首页 > 其他好文 > 详细

【SPOJ】Substrings(后缀自动机)

时间:2019-03-28 09:57:30      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:algorithm   size   algo   can   ++   long   i++   char s   queue   

/*
求right集合大小 
然后后缀最大值 

*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#define ll long long 
#define M 600010
#define mmp make_pair
using namespace std;
int read()
{
    int nm = 0, f = 1;
    char c = getchar();
    for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
    return nm * f;
}
char s[M];
int ch[M][26], sz[M], ans[M], tim[M], a[M], fa[M], len[M], cnt = 1, lst = 1; 

void insert(int c)
{
    int p = ++cnt, f = lst;
    lst = cnt;
    len[p] = len[f] + 1;
    sz[p] = 1;
    while(f && !ch[f][c]) ch[f][c] = p, f = fa[f];
    if(!f) fa[p] = 1;
    else
    {
        int q = ch[f][c];
        if(len[q] == len[f] + 1) fa[p] = q;
        else
        {
            int nq = ++cnt;
            fa[nq] = fa[q];
            len[nq] = len[f] + 1;
            memcpy(ch[nq], ch[q], sizeof(ch[nq]));
            fa[p] = fa[q] = nq;
            while(f && ch[f][c] == q) ch[f][c] = nq, f = fa[f];
        }
    }
}
    

int main()
{
    scanf("%s", s + 1);
    int l = strlen(s + 1);
    for(int i = 1; i <= l; i++) insert(s[i] - 'a');
    for(int i = 1; i <= cnt; i++) tim[len[i]]++;
    for(int i = 1; i <= cnt; i++) tim[i] += tim[i - 1];
    for(int i = 1; i <= cnt; i++) a[tim[len[i]]--] = i;
    for(int i = cnt; i >= 1; i--)
    {
        sz[fa[a[i]]] += sz[a[i]];
        ans[len[a[i]]] = max(ans[len[a[i]]], sz[a[i]]);
    }
    for(int i = cnt; i >= 1; i--) ans[i] = max(ans[i], ans[i + 1]);
    for(int i = 1; i <= l; i++) cout << ans[i] << "\n"; 
    return 0;
}

【SPOJ】Substrings(后缀自动机)

标签:algorithm   size   algo   can   ++   long   i++   char s   queue   

原文地址:https://www.cnblogs.com/luoyibujue/p/10612613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!