zoukankan      html  css  js  c++  java
  • 【洛谷P4081】【USACO17DEC】—Standing Out from the Herd(广义Sam)

    传送门

    建出广义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 fi first
    #define se second
    #define pb push_back
    #define cs const
    #define bg begin
    template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
    template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
    cs int N=100005;
    namespace Sam{
    	cs int M=N<<1;
    	int tot,len[M],fa[M],nxt[M][26],vit[M];
    	inline void init(){
    		tot=1;
    	}
    	inline int insert(int c,int last){
    		if(nxt[last][c]&&len[nxt[last][c]]==len[last]+1)return nxt[last][c];
    		int cur=++tot,p=last,fg=0;
    		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;
    				len[clo]=len[p]+1,fa[clo]=fa[q];
    				memcpy(nxt[clo],nxt[q],sizeof(nxt[q]));
    				if(len[clo]==len[cur])fg=1;
    				for(;p&&nxt[p][c]==q;p=fa[p])nxt[p][c]=clo;
    				fa[q]=fa[cur]=clo;
    			}
    		}
    		return fg?tot:cur;
    	}
    	inline void update(vector<int> &s,int id){
    		int p=1;
    		for(int &c:s){
    			p=nxt[p][c];
    			for(int x=p;x;x=fa[x])vit[x]=vit[x]?(vit[x]==id?id:-1):id;
    		}
    	}
    	inline int query(vector<int> &s){
    		int p=1,res=0;
    		for(int &c:s){
    			p=nxt[p][c];
    			for(int x=p;x;x=fa[x])if(~vit[x])res+=len[x]-len[fa[x]],vit[x]=-1;
    		}
    		return res;
    	}
    }
    namespace Trie{
    	int nxt[N][26],tot,pos[N];
    	inline void insert(char *s){
    		int p=0;
    		for(int i=1,len=strlen(s+1);i<=len;i++){
    			int c=s[i]-'a';
    			if(!nxt[p][c])nxt[p][c]=++tot;
    			p=nxt[p][c];
    		}
    	}
    	inline void build(){
    		queue<int> q;
    		Sam::init(),q.push(0),pos[0]=1;
    		while(!q.empty()){
    			int u=q.front();q.pop();
    			for(int i=0;i<26;i++){
    				int v=nxt[u][i];
    				if(v){
    					pos[v]=Sam::insert(i,pos[u]);
    					q.push(v);
    				}
    			}
    		}
    	}
    }
    int n;
    char s[N];
    vector<int> ss[N];
    int main(){
    	n=read();
    	for(int i=1;i<=n;i++){
    		scanf("%s",s+1);
    		Trie::insert(s);
    		for(int j=1,len=strlen(s+1);j<=len;j++)
    		ss[i].pb(s[j]-'a');
    	}
    	Trie::build();
    	for(int i=1;i<=n;i++)Sam::update(ss[i],i);
    	for(int i=1;i<=n;i++)cout<<Sam::query(ss[i])<<'
    ';
    }
    
  • 相关阅读:
    Node.js Net 模块+DNS 模块
    php程序报500错误
    Node.js 工具模块-OS模块+path模块
    Node.js GET/POST请求
    Canvas动画+canvas离屏技术
    Python OS 模块
    Python random 模块
    Python time 模块
    Python 迭代器
    Python 生成器
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328403.html
Copyright © 2011-2022 走看看