BZOJ-3473: 字符串(Suffix Array+Binary Search)

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3473

后缀数组。然后我这个傻叉没YY出O(n log n)的做法,只能手残了一个枚举每一个后缀,然后二分查找该后缀产生的最长符合条件的前缀,主席树维护查询操作的O(n log^2 n)的做法,然后又再次很长很慢的卡过去了额。。。(后来又YY了一下,好像枚举出改前缀之后,该前缀的所有位就没有必要枚举后缀了额。。。)

代码(巨丑无比,求大神轻喷):

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <cmath>

 

using namespace std ;

 

#define check( ch ) ( ch >= 'a' && ch <= 'z' )

#define inf 0x7fffffff

#define rep( i , x ) for ( int i = 0 ; i ++ < x ; )

#define down( i , x ) for ( int i = x ; i ; -- i )

 

typedef long long ll ;

 

const int maxn = 300100 ;

 

int Str[ maxn ] , len ;

 

void getstr(  ) {

    len = 0 ;

    int ch ; for ( ch = getchar(  ) ; ! check( ch ) ; ch = getchar(  ) ) ;

    Str[ ++ len ] = ch ;

    for ( ch = getchar(  ) ; check( ch ) ; ch = getchar(  ) ) Str[ ++ len ] = ch ;

}

 

int n , k , m = 0 , s[ maxn ] , first[ maxn ] , num[ maxn ] , last[ maxn ] ;

 

int Rank[ maxn ] , sa[ maxn ] , height[ maxn ] , x[ maxn ] , y[ maxn ] , w[ maxn ] , r[ maxn ] ;

 

void build_sa(  ) {

    int M = 0 ;

    rep( i , m ) M = max( M , Rank[ i ] = s[ i ] ) ;

    int N , b = 1 ;

    do {

        rep( i , m ) {

            x[ i ] = Rank[ i ] ;

            y[ i ] = i + b <= m ? Rank[ i + b ] : 0 ;

        }

        b <<= 1 ;

        rep( i , M + 1 ) w[ i - 1 ] = 0 ;

        rep( i , m ) w[ y[ i ] ] ++ ;

        rep( i , M ) w[ i ] += w[ i - 1 ] ;

        rep( i , m ) r[ w[ y[ i ] ] -- ] = i ;

        rep( i , M + 1 ) w[ i - 1 ] = 0 ;

        rep( i , m ) w[ x[ r[ i ] ] ] ++ ;

        rep( i , M ) w[ i ] += w[ i - 1 ] ;

        down( i , m ) sa[ w[ x[ r[ i ] ] ] -- ] = r[ i ] ;

        N = 0 ;

        rep( i , m ) {

            if ( i == 1 || x[ sa[ i ] ] != x[ sa[ i - 1 ] ] || y[ sa[ i ] ] != y[ sa[ i - 1 ] ] ) ++ N ;

            Rank[ sa[ i ] ] = N ;

        }

        M = N ;

    } while ( N < m ) ;

    int temp = 0 ;

    rep( i , m ) {

        height[ Rank[ i ] ] = temp ;

        for ( int j = temp ; i + j <= m && sa[ Rank[ i ] - 1 ] + j <= m && s[ i + j ] == s[ sa[ Rank[ i ] - 1 ] + j ] ; ++ j ) ++ height[ Rank[ i ] ] ;

        temp = max( 0 , height[ Rank[ i ] ] - 1 ) ;

    }

}

 

int st[ maxn ][ 21 ] , Stn ;

 

void Init_st(  ) {

    Stn = int( log2( m ) ) + 1 ;

    rep( i , m ) st[ i ][ 0 ] = height[ i ] ;

    rep( i , Stn ) rep( j , m ) {

        st[ j ][ i ] = min( st[ j ][ i - 1 ] , st[ j + ( 1 << ( i - 1 ) ) ][ i - 1 ] ) ;

    }

}

 

int Min( int l , int r ) {

    int k = int( log2( r - l + 1 ) ) ;

    return min( st[ l ][ k ] , st[ r - ( 1 << k ) + 1 ][ k ] ) ;

}

 

struct saver {

    int v , t ;

    void oper( int _v , int _t ) {

        v = _v , t = _t ;

    }

    bool operator < ( const saver &a ) const {

        return v < a.v || ( v == a.v && t < a.t ) ;

    }

} B[ maxn ] ;

 

int suff[ maxn ] ;

 

struct node {

    node *left , *right ;

    int s ;

    node(  ) {

        left = right = NULL ;

        s = 0 ;

    }

} *null = new( node ) ;

 

node *pre[ maxn ] ;

 

void Add( int x , int l , int r , node *u , node* &t ) {

    t = new( node ) ;

    t -> s = u -> s + 1 ;

    if ( l == r ) return ;

    int mid = ( l + r ) >> 1 ;

    if ( x <= mid ) {

        t -> right = u -> right ;

        Add( x , l , mid , u -> left , t -> left ) ;

    } else {

        t -> left = u -> left ;

        Add( x , mid + 1 , r , u -> right , t -> right ) ;

    }

}

 

void Init_sgt(  ) {

    rep( i , m ) B[ i ].oper( num[ sa[ i ] ] , i ) ;

    sort( B + 1 , B + m + 1 ) ;

    memset( suff , 0 , sizeof( suff ) ) ;

    rep( i , m ) {

        if ( i == m || B[ i ].v != B[ i + 1 ].v ) suff[ B[ i ].t ] = m + 1 ;

        else suff[ B[ i ].t ] = B[ i + 1 ].t ;

    }

    null -> left = null -> right = null ;

    pre[ 0 ] = null ;

    rep( i , m ) Add( suff[ i ] , 1 , m + 1 , pre[ i - 1 ] , pre[ i ] ) ;

}

 

int query_sgt( int l , int r , int L , int R , node *t ) {

    if ( l == L && r == R ) return t -> s ;

    int mid = ( L + R ) >> 1 ;

    if ( r <= mid ) return query_sgt( l , r , L , mid , t -> left ) ;

    if ( l > mid ) return query_sgt( l , r , mid + 1 , R , t -> right ) ;

    return query_sgt( l , mid , L , mid , t -> left ) + query_sgt( mid + 1 , r , mid + 1 , R , t -> right ) ;

}

 

int Query_sgt( int l , int r , int vl , int vr ) {

    int rec = query_sgt( vl , vr , 1 , m + 1 , pre[ r ] ) ;

    int ret = query_sgt( vl , vr , 1 , m + 1 , pre[ l - 1 ] ) ;

    return rec - ret ;

}

 

bool Check( int x , int pos ) {

    int left , right ;

    int l , r ;

    l = 0 , r = Rank[ pos ] ;

    while ( r - l > 1 ) {

        int mid = ( l + r ) >> 1 ;

        if ( Min( mid + 1 , Rank[ pos ] ) >= x ) r = mid ; else l = mid ;

    }

    left = r ;

    l = Rank[ pos ] , r = m + 1 ;

    while ( r - l > 1 ) {

        int mid = ( l + r ) >> 1 ;

        if ( Min( Rank[ pos ] + 1 , mid ) >= x ) l = mid ; else r = mid ;

    }

    right = l ;

    return Query_sgt( left , right , right + 1 , m + 1 ) >= k ;

}

 

int Query( int x ) {

    int L = 0 , R = m - x + 2 ;

    while ( R - L > 1 ) {

        int MID = ( L + R ) >> 1 ;

        if ( Check( MID , x ) ) L = MID ; else R = MID ;

    }

    return L ;

}

 

int main(  ) {

    scanf( "%d%d" , &n , &k ) ;

    memset( num , 0 , sizeof( num ) ) ;

    rep( i , n ) {

        getstr(  ) ;

        first[ i ] = m + 1 ;

        rep( j , len ) {

            s[ ++ m ] = Str[ j ] ;

            num[ m ] = i ;

        }

        s[ last[ i ] = ++ m ] = int( '$' ) ;

    }

    build_sa(  ) ;

    Init_st(  ) ;

    Init_sgt(  ) ;

    rep( i , n ) {

        ll ans = 0 ;

        for ( int j = first[ i ] ; s[ j ] != int( '$' ) ; ++ j ) {

            ans += ll( min( Query( j ) , last[ i ] - j ) ) ;

        }

        printf( "%lld " , ans ) ;

    }

    printf( "\n" ) ;

    return 0 ;

}
    原文作者:AmadeusChan
    原文地址: https://www.jianshu.com/p/bd670c566563
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞