• 【BZOJ2882】工艺(后缀自动机)


    【BZOJ2882】工艺(后缀自动机)

    题面

    BZOJ权限题,良心洛谷

    题解

    还是一样的,先把串在后面接一遍
    然后构建(SAM)
    直接按照字典序输出(n)次就行了

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<vector>
    #include<queue>
    using namespace std;
    #define ll long long
    #define RG register
    #define MAX 620000
    inline int read()
    {
        RG int x=0,t=1;RG char ch=getchar();
        while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
        if(ch=='-')t=-1,ch=getchar();
        while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
        return x*t;
    }
    struct Node
    {
    	map<int,int> son;
    	int ff,len;
    }t[MAX<<1];
    int last=1,tot=1;
    int n,a[MAX];
    void extend(int c)
    {
    	int p=last,np=++tot;last=np;
    	t[np].len=t[p].len+1;
    	while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
    	if(!p)t[np].ff=1;
    	else
    	{
    		int q=t[p].son[c];
    		if(t[q].len==t[p].len+1)t[np].ff=q;
    		else
    		{
    			int nq=++tot;
    			t[nq]=t[q];
    			t[nq].len=t[p].len+1;
    			t[q].ff=t[np].ff=nq;
    			while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
    		}
    	}
    }
    int main()
    {
    	n=read();
    	for(int i=1;i<=n;++i)a[i]=read();
    	for(int i=1;i<=n;++i)extend(a[i]);
    	for(int i=1;i<=n;++i)extend(a[i]);
    	for(int i=1,pos=1;i<=n;++i)
    	{
    		printf("%d ",t[pos].son.begin()->first);
    		pos=t[pos].son.begin()->second;
    	}
    	puts("");
    	return 0;
    }
    
    
  • 相关阅读:
    学习笔记之jq
    学习笔记之pytest
    本月学习小结(01/10
    本月学习小结(01/09
    学习笔记之Poetry
    学习笔记之Dynaconf
    学习笔记之asyncio — Asynchronous I/O
    Redis 根据key获取所有 HashKey
    Java ffmpeg 合成音视频文件
    Redis 存储中文方式
  • 原文地址:https://www.cnblogs.com/cjyyb/p/8457158.html
Copyright © 2020-2023  润新知