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

[BZOJ4516][SDOI2016]生成魔咒

时间:2018-03-27 22:00:26      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:++i   http   \n   class   题意   logs   zoj   math   多少   

bzoj
luogu

题意

求一个串每个前缀中含有多少个不同字串
\(n\le100000\),字符集大小\(10^9\)

sol

后缀自动机的转移开个\(map\)就好了。
每次插入以后,新增的贡献就是\(len[last]-len[fa[last]]\)
累加即可。

code

#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
int gi()
{
    int x=0,w=1;char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-') w=0,ch=getchar();
    while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return w?x:-x;
}
const int N = 2e5+5;
int n,last=1,tot=1,fa[N],len[N];long long ans;
map<int,int>tr[N];
void extend(int c)
{
    int v=last,u=++tot;last=u;
    len[u]=len[v]+1;
    while (v&&!tr[v][c]) tr[v][c]=u,v=fa[v];
    if (!v) fa[u]=1;
    else
    {
        int x=tr[v][c];
        if (len[x]==len[v]+1) fa[u]=x;
        else
        {
            int y=++tot;
            tr[y]=tr[x];
            fa[y]=fa[x];fa[x]=fa[u]=y;len[y]=len[v]+1;
            while (v&&tr[v][c]==x) tr[v][c]=y,v=fa[v];
        }
    }
}
int main()
{
    n=gi();
    for (int i=1;i<=n;++i)
    {
        int x=gi();
        extend(x);
        printf("%lld\n",ans+=len[last]-len[fa[last]]);
    }
    return 0;
}

[BZOJ4516][SDOI2016]生成魔咒

标签:++i   http   \n   class   题意   logs   zoj   math   多少   

原文地址:https://www.cnblogs.com/zhoushuyu/p/8660281.html

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