zoukankan      html  css  js  c++  java
  • loj#6041. 「雅礼集训 2017 Day7」事情的相似度(后缀自动机+启发式合并)

    题面

    传送门

    题解

    为什么成天有人想搞些大新闻

    这里写的是(yyb)巨巨说的启发式合并的做法(虽然(LCT)的做法不知道比它快到哪里去了……)

    建出(SAM),那么两个前缀的最长公共后缀就是它们在(parent)树上的(LCA)的深度

    对于每一个子串来说,所有和它相同的串里只有它的前驱和它的后继是有贡献的。这个只要考虑任何一个包含了这个子串和另外一个和它相同的子串的询问,这个询问必定包含它的前驱或后继

    那么我们用(set)启发式合并来维护(endpos)集合,每一次只要把把两个最靠近的点对用来更新答案就好了

    然后把所有的询问离线,扫描线+树状数组就行了,这个可以看代码

    //minamoto
    #include<bits/stdc++.h>
    #define R register
    #define IT set<int>::iterator
    #define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
    #define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
    #define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
    template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
    using namespace std;
    char buf[1<<21],*p1=buf,*p2=buf;
    inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
    int read(){
        R int res,f=1;R char ch;
        while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
        for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
        return res*f;
    }
    int read(char *s){
    	R int len=0;R char ch;while(((ch=getc())>'9'||ch<'0'));
    	for(s[++len]=ch;(ch=getc())>='0'&&ch<='9';s[++len]=ch);
    	return s[len+1]='',len;
    }
    char sr[1<<21],z[20];int C=-1,Z=0;
    inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
    void print(R int x){
        if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
        while(z[++Z]=x%10+48,x/=10);
        while(sr[++C]=z[Z],--Z);sr[++C]='
    ';
    }
    const int N=2e5+5,M=N*20;
    struct eg{int v,nx;}e[N<<1];int head[N],tot;
    inline void Add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
    struct Ask{
    	int l,r,id;
    	inline bool operator <(const Ask &b)const{return r<b.r;}
    }q[N];
    struct node{
    	int x,y,w;
    	node(){}
    	node(R int xx,R int yy,R int ww):x(xx),y(yy),w(ww){}
    	inline bool operator <(const node &b)const{return y<b.y;}
    }st[M];
    char t[N];int fa[N],ch[N][2],l[N],sz[N],c[N],rt[N],ans[N];
    set<int>s[N];int cnt=1,las=1,n,m,top;
    inline void upd(R int x,R int y){for(;x;x-=x&-x)cmax(c[x],y);}
    inline int ask(R int x){R int res=0;for(;x<=n;x+=x&-x)cmax(res,c[x]);return res;}
    void ins(int c){
    	int p=las,np=las=++cnt;l[np]=l[p]+1;
    	for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
    	if(!p)fa[np]=1;
    	else{
    		int q=ch[p][c];
    		if(l[q]==l[p]+1)fa[np]=q;
    		else{
    			int nq=++cnt;l[nq]=l[p]+1;
    			memcpy(ch[nq],ch[q],8);
    			fa[nq]=fa[q],fa[q]=fa[np]=nq;
    			for(;p&&ch[p][c]==q;p=fa[p])ch[p][c]=nq;
    		}
    	}
    }
    void merge(int p,int q,int len){
    	IT it,nxt;
    	for(it=s[q].begin();it!=s[q].end();++it){
    		nxt=s[p].lower_bound(*it);
    		nxt!=s[p].end()?(st[++top]=node(*it,*nxt,len),0):0;
    		nxt!=s[p].begin()?(st[++top]=node(*--nxt,*it,len),0):0;
    	}
    	for(it=s[q].begin();it!=s[q].end();++it)s[p].insert(*it);
    }
    void dfs(int u){
    	sz[u]=1;
    	go(u){
    		dfs(v),sz[u]<sz[v]?(swap(rt[u],rt[v]),0):0;
    		merge(rt[u],rt[v],l[u]),sz[u]+=sz[v];
    	}
    }
    int main(){
    //	freopen("testdata.in","r",stdin);
    	n=read(),m=read(),read(t);
    	fp(i,1,n)ins(t[i]-'0'),s[las].insert(i);
    	fp(i,2,cnt)Add(fa[i],i);
    	fp(i,1,cnt)rt[i]=i;
    	go(1)dfs(v);
    	fp(i,1,m)q[i].l=read(),q[i].r=read(),q[i].id=i;
    	sort(q+1,q+1+m),sort(st+1,st+1+top);
    	for(R int i=1,j=1,k=1;i<=n;++i){
    		while(j<=top&&st[j].y<=i)upd(st[j].x,st[j].w),++j;
    		while(k<=m&&q[k].r==i)ans[q[k].id]=ask(q[k].l),++k;
    	}
    	fp(i,1,m)print(ans[i]);
    	return Ot(),0;
    }
    
  • 相关阅读:
    js积累点
    org.hibernate.NonUniqueObjectException
    SSH框架下的表单重复提交
    ajax传输中文参数乱码,本地使用tomcat不乱码,liunx+weblogic乱码
    启动weblogic报错:string value '2.4' is not a valid enumeration value for web-app-versionType in namespace http://java.sun.com/xml/ns/javaee
    weblogic启动项目,设置内容、设置的数据源链接不生效
    myeclipse编译弹框:The builder launch configuration could not be found
    tomcat启动项目报错:The specified JRE installation does not exist
    Mac上的jdk
    前端
  • 原文地址:https://www.cnblogs.com/bztMinamoto/p/10522716.html
Copyright © 2011-2022 走看看