zoukankan      html  css  js  c++  java
  • cf700E. Cool Slogans

    题解: 首先建出后缀自动机 我们考虑对于每个节点i 在其parent树上找到离其最近的j让s[j]子串在s[i]中出现>=2次以上(因为在j祖先节点必然都满足条件 但显然最近的最优) 所以把原parent树转化成新树 新树的最大高度即为答案 具体实现 可以倍增+线段树合并

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <cmath>
    #include <set>
    #include <map>
    #define mp make_pair
    #define pb push_back
    #define pii pair<int,int>
    #define link(x) for(edge *j=h[x];j;j=j->next)
    #define inc(i,l,r) for(int i=l;i<=r;i++)
    #define dec(i,r,l) for(int i=r;i>=l;i--)
    const int MAXN=4e5+10;
    const double eps=1e-8;
    #define ll long long
    using namespace std;
    struct edge{int t;edge*next;}e[MAXN<<1],*h[MAXN],*o=e;
    void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
    ll read(){
        ll x=0,f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
        return x*f;
    }
    
    
    
    int sz[MAXN],dis[MAXN],fa[MAXN],ch[MAXN][26];
    int rt,cur,cnt,Rt[MAXN],n,cnt1;
    vector<int>vec[MAXN];
    char str[MAXN];
    typedef struct node{
        int l,r,num;
    }node;
    node d[MAXN*42];
    void update(int &x,int l,int r,int t){
        if(!x){x=++cnt1;d[x].l=0;d[x].r=0;d[x].num=0;}
        if(l==r){d[x].num=1;return ;}
        int mid=(l+r)>>1;
        if(t<=mid)update(d[x].l,l,mid,t);
        else update(d[x].r,mid+1,r,t);
        d[x].num=d[d[x].l].num+d[d[x].r].num;
    }
    void merge(int &x,int y,int l,int r){
        if(!y)return ;
        if(!x){x=y;return ;}
        int t=++cnt1;d[t]=d[x];d[t].num+=d[y].num;x=t;
        if(l==r)return ;
        int mid=(l+r)>>1;
        merge(d[x].l,d[y].l,l,mid);
        merge(d[x].r,d[y].r,mid+1,r);
    }
    int ans;
    void querty(int x,int l,int r,int ql,int qr){
        if(!x)return ;
        if(ql<=l&&r<=qr){ans+=d[x].num;return ;}
        int mid=(l+r)>>1;
        if(ql<=mid)querty(d[x].l,l,mid,ql,qr);
        if(qr>mid)querty(d[x].r,mid+1,r,ql,qr);
    }
    
    void built(int x){
        int last=cur;cur=++cnt;dis[cur]=dis[last]+1;int p=last;sz[cur]=dis[cur];
        update(Rt[cur],1,n,dis[cur]);
        for(;p&&!ch[p][x];p=fa[p])ch[p][x]=cur;
        if(!p)fa[cur]=rt;
        else{
    	int q=ch[p][x];
    	if(dis[q]==dis[p]+1)fa[cur]=q;
    	else{
    	    int nt=++cnt;dis[nt]=dis[p]+1;
    	    memcpy(ch[nt],ch[q],sizeof(ch[q]));
    	    fa[nt]=fa[q];fa[q]=fa[cur]=nt;
    	    for(;ch[p][x]==q;p=fa[p])ch[p][x]=nt;
    	}
        }
    }
    int f[MAXN][21],dep[MAXN];
    
    void dfs(int x,int pre,int deep){
        f[x][0]=pre;dep[x]=deep+1;
      //  cout<<x<<"===="<<pre<<endl;
        link(x){
    	dfs(j->t,x,deep+1);
    	sz[x]=sz[j->t];
    	merge(Rt[x],Rt[j->t],1,n);
        }
     //   cout<<x<<"===="<<Rt[x]<<" "<<d[Rt[x]].num<<endl;
    }
    int Lca(int x,int t){
        for(int i=0;i<=20;i++)if(t&(1<<i))x=f[x][i];
        return x;
    }
    bool check(int l,int r,int x,int t){
        int t1=Lca(x,t);
        //cout<<fa[t1]<<" "<<dis[fa[t1]]<<endl;
        ans=0;querty(Rt[t1],1,n,l+dis[fa[t1]],r);
        //cout<<l+dis[fa[t1]]<<" "<<r<<" "<<x<<" "<<t<<" "<<t1<<"::: "<<ans<<endl;
        return (ans>=2);
    }
    
    void slove(int x,int t){
        if(!t)return ;
        //cout<<x<<"===="<<t<<endl;
        int l=1;int r=t;int ans1=0;
        //cout<<sz[x]-dis[x]+1<<" "<<sz[x]<<" "<<x<<" "<<t<<endl;
        while(l<=r){
    	int mid=(l+r)>>1;
    	if(check(sz[x]-dis[x]+1,sz[x],x,mid))ans1=mid,r=mid-1;
    	else l=mid+1;
        }
        int t1=Lca(x,ans1);
        if(t1==x)t1=rt;
        vec[t1].pb(x);
      //  cout<<t1<<"-----"<<x<<endl;
    }
    void dfs1(int x){
        inc(i,1,20)f[x][i]=f[f[x][i-1]][i-1];
        if(x!=rt)slove(x,dep[x]-1);
        link(x){
    	dfs1(j->t);
        }
    }
    int maxx;
    void dfs2(int x,int deep){
        maxx=max(maxx,deep);
        for(int i=0;i<vec[x].size();i++){
    	dfs2(vec[x][i],deep+1);
        }
    }
    
    int main(){
        n=read();scanf("%s",str+1);
        cnt=cur=rt=1;
        inc(i,1,n)built(str[i]-'a');
        inc(i,1,cnt)add(fa[i],i);
        dfs(rt,0,0);dfs1(rt);
      //  cout<<"sb"<<endl;
        maxx=0;dfs2(rt,0);
        printf("%d
    ",maxx);
    }
    

      

                      E. Cool Slogans
                      time limit per test
                      4 seconds
                      memory limit per test
                      512 megabytes
    input
    standard input
    output
    standard output

    Bomboslav set up a branding agency and now helps companies to create new logos and advertising slogans. In term of this problems, slogan of the company should be a non-empty substring of its name. For example, if the company name is "hornsandhoofs", then substrings "sand" and "hor" could be its slogans, while strings "e" and "hornss" can not.

    Sometimes the company performs rebranding and changes its slogan. Slogan A is considered to be cooler than slogan B if B appears in Aas a substring at least twice (this occurrences are allowed to overlap). For example, slogan A =  "abacaba" is cooler than slogan B = "ba", slogan A =  "abcbcbe" is cooler than slogan B =  "bcb", but slogan A =  "aaaaaa" is not cooler than slogan B =  "aba".

    You are given the company name w and your task is to help Bomboslav determine the length of the longest sequence of slogans s1, s2, ..., sk, such that any slogan in the sequence is cooler than the previous one.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the company name that asks Bomboslav to help. The second line contains the string w of length n, that consists of lowercase English letters.

    Output

    Print a single integer — the maximum possible length of the sequence of slogans of the company named w, such that any slogan in the sequence (except the first one) is cooler than the previous

  • 相关阅读:
    我的博客开通了啦!
    今天终于下定决心,辞掉现在的工作,开始导找新的机会。
    C# 文件流的使用
    XNA学习笔记(二) 发布release版本出现的问题
    Unity3D学习笔记(三) 数组和容器(泛型)使用学习(基于C#)
    Unity3D学习笔记(六) 关于碰撞
    Unity3D学习笔记(一) 模型和贴图导入学习
    Unity3D学习笔记(八) 保存数据的简单方式
    Unity3D学习笔记(五) C#基础学习
    Unity3D学习笔记(四) 脚本内访问其他对象,组件或脚本成员方法总结
  • 原文地址:https://www.cnblogs.com/wang9897/p/10066108.html
Copyright © 2011-2022 走看看