bzoj 2806 后缀自动机 + 单调队列dp + 性质

题意:将 一个字符串分为几段,且其中长度 大于 L 且在文本库中出现的串的长度 大于等于90%,求满足条件的最大L

思路:好题好题

1.至少这种东西8成是二分(二分不要总想等于)

2.考虑用dp对每一个L进行验证。这里的难点是发现dp的转移具有单调性,不太明显的单调性。

主要用到 一个性质,相邻的两个前缀在另一个字符串中的最长公共后缀的 差( 后 - 前  ) <= 1

3.另一个坑了我好久的东西就是如何求 一个前缀在另一个字符串中出现的最长后缀。

走过失配边后不能先向下走,要先统计答案。

4.记住单调队列dp的精髓,两个决策指针都是单调的。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 5000005;
const int ch_size = 2;
struct SAM{
    int fa[maxn<<1],    // 后缀链接
            ch[maxn<<1][ch_size],
            len[maxn<<1],   //该节点最长串的长度
            tot,    // 节点总数
            last;  // 代表当前的整个串
    void init(){
        tot = last = 0;
        fa[0] = -1;
        len[0] = 0;
        memset( ch[0],0,sizeof( ch[0] ) );
    }
    void extend( int x ){
        int p = last;
        if( ch[p][x] ){
            int q = ch[p][x];
            if( len[q] == len[p]+1 ) last = q;
            else{
                int clone = ++tot;
                len[clone] = len[p] + 1;
                last = clone;
                for (int i = 0; i < ch_size; i++) {
                    ch[clone][i] = ch[q][i];
                }
                fa[clone] = fa[q];
                fa[q]  = clone;
                while (p != -1) {
                    if (ch[p][x] == q)ch[p][x] = clone;
                    else break;
                    p = fa[p];
                }
            }
            return;
        }
        int cur = ++tot;
        memset( ch[cur],0,sizeof( ch[cur] ) );
        len[cur] = len[last]+1;
        while( p != -1 && !ch[p][x] ){
            ch[p][x] = cur;
            p  = fa[p];
        }
        if( p == -1 ){
            fa[cur] = 0;
        }else {
            int q = ch[p][x];
            if (len[q] == len[p] + 1) {
                fa[cur] = q;
            } else {
                int clone = ++tot;
                len[clone] = len[p] + 1;
                for (int i = 0; i < ch_size; i++) {
                    ch[clone][i] = ch[q][i];
                }
                fa[clone] = fa[q];
                fa[q] = fa[cur] = clone;
                while (p != -1) {
                    if (ch[p][x] == q)ch[p][x] = clone;
                    else break;
                    p = fa[p];
                }
            }
        }
        last = cur;
    }
}g;
char str[maxn],str2[maxn];
int L[maxn],dp[maxn],Len,Q[maxn],head,tail;
bool judge( int len ){
    head = 1,tail = 0;
    //Q[++tail] = 0;
    for( int i = 1;i <= Len;i++ ){
        dp[i] = dp[i-1];
        if( i - len <0 ) continue;
        while( head <= tail && dp[ Q[tail] ]-Q[tail] < dp[i-len]-i+len ) tail--;
        Q[++tail] = i-len;
        while( head <= tail && Q[head] < i-L[i] ) head++;
        if( head <= tail ) dp[i] = max( dp[i],dp[ Q[head] ]+i-Q[head] );
    }
    if( 10*dp[Len] >= 9*Len ) return true;
    return false;
}
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    g.init();
    for( int i = 1;i <= m;i++ ){
        scanf("%s",str);
        int len = strlen(str);
        g.last = 0;
        for( int j = 0;j < len;j++ ){
            g.extend( str[j]-'0' );
        }
    }
    for( int i = 0;i < n;i++ ){
        scanf("%s",str2+1);
        Len = strlen( str2+1 );
        int p = 0,sum = 0;
        for( int j = 1;j <= Len;j++ ){
            int c = str2[j]-'0';
            if( g.ch[p][c] ){
                p =g.ch[p][c];sum++;
            }else{
                while( p != -1 && !g.ch[p][c] ) p = g.fa[p];
                if( p != -1 ) {
                    sum = g.len[p]+1;p=g.ch[p][c];
                }else sum = p = 0;
            }
            L[j] = sum;
        }
        int l = 0,r = Len+1;
        while( l != r-1 ){
            int mid = l+r >>1;
            if( judge(mid) ){
                l = mid;
            }else r = mid;
        }
        printf("%d\n",l);
    }
    return 0;
};

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值