POJ 2774( 后缀数组模板 )

注意 :swap( x[N],y[N] ) 复杂度为O(N),不要直接swap


//最长公共子串

#include 

#include

#include

using namespace std;



typedef long long LL;



typedef int lint;



const lint maxn = 1000000 + 10;



char s[maxn];



struct suffix{



    lint c[maxn],sa[maxn],t1[maxn],t2[maxn],m,n,h[maxn],height[maxn],rk[maxn];



    void Build_SA(){

        m = 0;

	lint* x = t1,*y = t2;

        n =strlen(s);

        for( lint i = 0;i < n;i++ ) m = max( m,(lint)s[i] );

        for( lint i = 0;i <= m;i++ ) c[i] = 0;

        for( lint i = 0;i < n;i++ ) c[ x[i] = s[i] ]++;

        for( lint i = 1;i <= m;i++ ) c[i] += c[i-1];

        for( lint i = n-1;i >= 0;i-- ) {

            sa[ --c[ x[i] ] ] = i;

        }

        for( lint k = 1;k < n;k <<= 1 ){

            lint cnt = 0;

            for( lint i = n - 1;i >= n-k;i-- ) y[cnt++] = i; //

            for( lint i = 0;i < n;i++ ) if( sa[i] >= k ) y[cnt++] = sa[i]-k;

            for( lint i = 0;i <= m;i++ ) c[i] = 0;

            for( lint i = 0;i < n;i++ ) c[ x[i] ]++;

            for( lint i = 1;i <= m;i++ ) c[i] += c[i-1];

            for( lint i = cnt-1;i >= 0;i-- ) sa[ --c[ x[ y[i] ] ] ] = y[i];

            swap( x,y );

            lint num = 0;

            x[ sa[0] ] = 0;

            for( lint i = 1;i < n;i++ ){

                if( y[ sa[i-1] ] != y[ sa[i] ] || y[ sa[i-1]+k ] != y[ sa[i]+k ] ){

                    x[ sa[i] ] = ++num;

                }else{

                    x[ sa[i] ] = num;

                }

            }

            if( num == n-1 ) return;

            m = num;

        }

    }

    void getheight(){

        for( lint i = 0;i < n;i++ ){

            rk[ sa[i] ] = i;

        }

        lint cnt = 1;

        h[ sa[0] ] = 0;

        height[ 0 ] = 0;

        for( lint i = 0;i < n;i++ ){

            if(cnt)cnt--;

            while(  rk[i] >= 1 &&sa[rk[i]-1]+cnt < n && i + cnt < n && s[ sa[rk[i]-1]+cnt ] == s[ i+cnt ] ) cnt++;

            h[ i ] = cnt;

            height[ rk[i] ] = cnt;

        }

        //cout << "debug" << endl;

    }



}g;

char str1[maxn],str2[maxn];

lint len1,len2;

bool solve( lint len ){

    bool flag1 = false,flag2 = false;

    for( lint i = 0;i < g.n;i++ ){

        if( i == 0 || g.height[i] < len ){

            flag1 = false;

            flag2 = false;

        }

        if( g.sa[i] < len1 ){

            flag1 = true;

        }else if( g.sa[i] > len1 ) {

            flag2 = true;

        }

        if( flag1 && flag2 ) return true;

    }

    return false;

}

int main()



{

    scanf("%s%s",s,str2);

    len1 = strlen( s );

    len2 = strlen( str2 );

    strcat( s,"A" );

    strcat( s,str2 );

    g.Build_SA();

    g.getheight();

    lint ans = 0;

    for( lint i = 1;i < g.n;i++ ){

        bool flag1 =  g.sa[i]  < len1;

        bool flag2 = g.sa[i-1] < len1;

        if( flag1 != flag2 ){

            ans = max( ans,g.height[i] );

        }

    }

    printf("%d",ans);

    return 0;



}

 

你可能感兴趣的:(字符串,后缀数组,模板)