zoukankan      html  css  js  c++  java
  • [bzoj4516] [Sdoi2016]生成魔咒

    后缀自动机裸题.

    每次extend后,最后加入的节点对答案的贡献是len[np]-len[pre[np]].

    因为根据后缀自动机的性质,最后加入的节点np的right集是最后加入的字符的位置.

    这个节点代表的状态也就是新后缀的状态.

    字符集很大,干脆用map模拟一下.

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    #include<string>
    #include<iomanip>
    #include<algorithm>
    #include<map>
    using namespace std;
    #define LL long long
    #define FILE "dealing"
    #define up(i,j,n) for(int i=j;i<=n;++i)
    #define db double
    #define eps 1e-10
    #define pii pair<int,int>
    int read(){
    	int x=0,f=1,ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    	return f*x;
    }
    const int maxn=200200,mod1=39989,mod2=(int)(1e9+0.1);
    int ans=0;
    namespace SAM{
    	int pre[maxn],len[maxn],now,cnt,p,np,q,nq,last;
    	map<int,int> c[maxn];
    	int extend(int x){
    		p=now;now=np=++cnt;len[np]=len[p]+1;last=np;
    		while(p&&!c[p].count(x)){
    			c[p][x]=np;
    			p=pre[p];
    		}
    		if(!p)pre[np]=1;
    		else {
    			q=c[p][x];
    			if(len[q]==len[p]+1)pre[np]=q;
    			else {
    				len[nq=++cnt]=len[p]+1;
    				map<int,int>::iterator it;
    				for(it=c[q].begin();it!=c[q].end();it++)
    					c[nq][it->first]=it->second;
    				pre[nq]=pre[q];
    				pre[q]=pre[np]=nq;
    				while(p&&c[p][x]==q)c[p][x]=nq,p=pre[p];
    			}
    		}
    	}
    };
    int main(){
    	//freopen(FILE".in","r",stdin);
    	//freopen(FILE".out","w",stdout);
    	int n=read();
    	using namespace SAM;
    	now=cnt=1;
    	up(i,1,n){
    		int x=read();
    		extend(x);
    		ans+=len[np]-len[pre[np]];
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    mysql中的几种join 及 full join问题
    MySQL基础练习题
    SQL之IFNULL()
    SQL之查找表中字段的值相同的记录
    Mysql之将一张表内容导入另一张表中
    selenium无界面操作浏览器与Chrome Options的启动项设置
    SQL UNION 和 UNION ALL 操作符
    Python断言方法assert
    Python标准库--contextlib模块
    Python标准库--itertools模块
  • 原文地址:https://www.cnblogs.com/chadinblog/p/6518877.html
Copyright © 2011-2022 走看看