zoukankan      html  css  js  c++  java
  • [BZOJ4516][SDOI2016]生成魔咒

    bzoj
    luogu

    题意

    求一个串每个前缀中含有多少个不同字串
    (nle100000),字符集大小(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
    ",ans+=len[last]-len[fa[last]]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    寒假作业3
    寒假作业2
    寒假作业
    Binary Indexed Tree
    Quick Union
    Prim's Algorithm & Kruskal's algorithm
    面向对象阶段总结 | 肆
    面向对象阶段总结 | 叁
    面向对象阶段总结 | 贰
    面向对象阶段总结 | 壹
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8660281.html
Copyright © 2011-2022 走看看