[LOJ 6288]猫咪[CF 700E]Cool Slogans

[LOJ 6288]猫咪[CF 700E]Cool Slogans

题意

给定一个字符串 \(T\), 求一个最大的 \(K\) 使得存在 \(S_1,S_2,\dots,S_k\) 满足 \(S_1\)\(T\) 的子串且 \(\forall 1\le i< k\)\(S_{i+1}\)\(S\)双子串.

其中双子串的定义是: 若 \(a\)\(b\) 的至少两个不同位置作为子串出现则 \(a\)\(b\) 的双子串. 出现位置可以重叠.

\(|T|\le 2\times 10^5\).

题解

考试的时候被沙雕题目描述搞得迷迷糊糊的结果没发现是原题←菜的真实

显然对于每个子串我们可以这个子串里挑一个答案最大且的双子串作为这个子串的答案.

我们发现当我们挑出一个子串 \(S_1\) 后, 可以通过对左右端点进行适当缩减来刚好卡到"每一个双子串都是上一个串的一个border"的程度. 因为如果出现了不是border的情况, 可以把前面所有的 \(S_i\) 全都缩短, 不难发现这样并不会导致答案变劣.

那么实际上要计算的就是每个子串最多迭代几个border. 考虑在SAM上DP. SAM的结点上只能控制右端点, 左端点是一个区间, 不难发现只要搞右端点相同就可以了. 线段树合并搞出每个点的 right 集合, 判断一下答案最大的祖先是否能对当前点做出贡献就可以了. 能做出贡献就 \(+1\), 否则就不加.

至于判断, 因为当前 right 集合对应的长度最大为 len 的子串都是完全一样的, 所以随便取一个位置判断就可以了.

参考代码

#include <bits/stdc++.h>

const int MAXN=4e5+10;

struct Node{
    int l;
    int r;
    int cnt;
    Node* lch;
    Node* rch;
    Node(int,int);
    void Insert(int);
    int Query(int,int);
};
Node* N[MAXN];

int n;
int cnt=1;
int root=1;
int last=1;
int s[MAXN];
int prt[MAXN];
int len[MAXN];
int val[MAXN];
int pos[MAXN];
int tprt[MAXN];
char str[MAXN];
std::map<char,int> chd[MAXN];

int Extend(char);
Node* Merge(Node*,Node*);

int main(){
    scanf("%s",str+1);
    n=strlen(str+1);
    for(int i=1;i<=n;i++){
        int x=Extend(str[i]);
        N[x]->Insert(pos[x]=i);
    }
    for(int i=1;i<=cnt;i++)
        s[i]=i;
    std::stable_sort(s+1,s+cnt+1,[](int a,int b){return len[a]>len[b];});
    for(int i=1;i<cnt;i++){
        N[prt[s[i]]]=Merge(N[prt[s[i]]],N[s[i]]);
        pos[prt[s[i]]]=pos[s[i]];
    }
    int ans=0;
    for(int i=cnt-1;i>=1;i--){
        int p=s[i];
        if(prt[p]==root){
            val[p]=1;
            tprt[p]=p;
        }
        else{
            assert(N[p]->Query(pos[p],pos[p]));
            int last=tprt[prt[p]];
            int cnt=N[last]->Query(pos[p]-len[p]+(len[prt[last]]+1),pos[p]);
            if(cnt>=2){
                val[p]=val[last]+1;
                tprt[p]=p;
            }
            else{
                val[p]=val[last];
                tprt[p]=last;
            }
        }
        ans=std::max(ans,val[p]);
    }
    printf("%d\n",ans);
    return 0;
}

void Node::Insert(int x){
    ++this->cnt;
    if(this->l!=this->r){
        int mid=(this->l+this->r)>>1;
        if(x<=mid){
            if(this->lch==NULL)
                this->lch=new Node(this->l,mid);
            this->lch->Insert(x);
        }
        else{
            if(this->rch==NULL)
                this->rch=new Node(mid+1,this->r);
            this->rch->Insert(x);
        }
    }
}

int Extend(char x){
    int p=last;
    int np=++cnt;
    N[last=np]=new Node(1,n);
    len[np]=len[p]+1;
    while(p&&!chd[p].count(x))
        chd[p][x]=np,p=prt[p];
    if(!p)
        prt[np]=root;
    else{
        int q=chd[p][x];
        if(len[q]==len[p]+1)
            prt[np]=q;
        else{
            int nq=++cnt;
            N[nq]=new Node(1,n);
            len[nq]=len[p]+1;
            chd[nq]=chd[q];
            prt[nq]=prt[q];
            prt[q]=nq;
            prt[np]=nq;
            while(p&&chd[p][x]==q)
                chd[p][x]=nq,p=prt[p];
        }
    }
    return np;
}

Node* Merge(Node* a,Node* b){
    if(a==NULL)
        return b;
    if(b==NULL)
        return a;
    Node* cur=new Node(a->l,b->r);
    cur->cnt=a->cnt+b->cnt;
    cur->lch=Merge(a->lch,b->lch);
    cur->rch=Merge(a->rch,b->rch);
    return cur;
}

int Node::Query(int l,int r){
    if(l<=this->l&&this->r<=r)
        return this->cnt;
    else{
        int ans=0;
        if(this->lch&&l<=this->lch->r)
            ans+=this->lch->Query(l,r);
        if(this->rch&&this->rch->l<=r)
            ans+=this->rch->Query(l,r);
        return ans;
    }
}

Node::Node(int l,int r):l(l),r(r),cnt(0),lch(NULL),rch(NULL){}

o_71393019_p0.png

转载于:https://www.cnblogs.com/rvalue/p/10951825.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值