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


    传送门

    把串倍长建SamSam即可

    #include<bits/stdc++.h>
    using namespace std;
    const int RLEN=1<<20|1;
    inline char gc(){
        static char ibuf[RLEN],*ib,*ob;
        (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
        return (ob==ib)?EOF:*ib++;
    }
    #define gc getchar
    inline int read(){
        char ch=gc();
        int res=0,f=1;
        while(!isdigit(ch))f^=ch=='-',ch=gc();
        while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
        return f?res:-res;
    }
    #define ll long long
    #define re register
    #define pii pair<int,int>
    #define pic pair<int,char>
    #define fi first
    #define se second
    #define pb push_back
    #define cs const
    #define bg begin
    #define poly vector<int>
    template<class T>inline void chemx(T &a,T b){a<b?a=b:0;}
    template<class T>inline void chemn(T &a,T b){a>b?a=b:0;}
    cs int N=600005;
    int n;
    int a[N];
    namespace Sam{
    	cs int M=N<<1;
    	map<int,int> nxt[M];
    	int len[M],last,tot,fa[M],pos[M];
    	inline void init(){
    		last=tot=1;
    	}
    	inline void insert(int c){
    		int cur=++tot,p=last;last=cur;
    		len[cur]=len[p]+1;
    		for(;p&&!nxt[p][c];p=fa[p])nxt[p][c]=cur;
    		if(!p)fa[cur]=1;
    		else{
    			int q=nxt[p][c];
    			if(len[p]+1==len[q])fa[cur]=q;
    			else{
    				int clo=++tot;
    				fa[clo]=fa[q],len[clo]=len[p]+1;
    				nxt[clo]=nxt[q];
    				for(;p&&nxt[p][c]==q;p=fa[p])nxt[p][c]=clo;
    				fa[cur]=fa[q]=clo;
    			}
    		}
    	}
    	void dfs(int u,int dep){
    		cout<<nxt[u].bg()->fi;
    		if(dep==n)cout<<'
    ',exit(0);
    		cout<<" ";dfs(nxt[u].bg()->se,dep+1);
    	}
    	inline void calc(){
    		dfs(1,1);
    	}
    }
    int main(){
    	n=read();
    	Sam::init();
    	for(int i=1;i<=n;i++){
    		a[i]=a[i+n]=read();
    	}
    	for(int i=1;i<=2*n;i++){
    		Sam::insert(a[i]);
    	}
    	Sam::calc();
    }
    
  • 相关阅读:
    tabbar 旋转指定的页面
    GDAL中文路径不能打开&Shp文件字段属性值中文乱码
    Project : error PRJ0019: 工具从"Moc'ing xxx.h..."
    详解Android中的屏幕方向
    qt中获取文件路径和文件名
    vs2005下Qt项目中修改exe图标的方法
    Qt & C/C++统计运行时间
    Qt 中Treewidget添加右键菜单
    QT 中文乱码解决方案
    Qt多线程应用QRunnable显示进度条示例
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328434.html
Copyright © 2020-2023  润新知