Data Structures/Suffix Array + LCP Array.cpp
// Build the SA (sorted array of suffixes)
Build the SA (sorted array of suffixes)
Build the SA ( sorted array of suffixes )
void buildSA()
{
    for(int i = 0; i <= n-1; i++)
    {
        SA[i] = i;
        buck[i] = s[i];
    }
    for(it = 1; it <= 2*n; it *= 2)
    {
        sort(SA, SA + n, suffcmp);
        for(int i = 0; i <= n - 2; i++)
        {
            tmp[i+1] = tmp[i];
            if( suffcmp(SA[i], SA[i+1]) )
                tmp[i+1]++;
        }
        for(int i = 0; i <= n - 1; i++)
        {
            buck[ SA[i] ] = tmp[i];
        }
        if( tmp[n - 1] == n - 1)
            break;
    }
}
void buildSA ( ) { for ( int i = 0 ; i <= n - 1 ; i ++ ) { SA [ i ] = i ; buck [ i ] = s [ i ] ; } for ( it = 1 ; it <= 2 * n ; it *= 2 ) { sort ( SA , SA + n , suffcmp ) ; for ( int i = 0 ; i <= n - 2 ; i ++ ) { tmp [ i + 1 ] = tmp [ i ] ; if ( suffcmp ( SA [ i ] , SA [ i + 1 ] ) ) tmp [ i + 1 ] ++ ; } for ( int i = 0 ; i <= n - 1 ; i ++ ) { buck [ SA [ i ] ] = tmp [ i ] ; } if ( tmp [ n - 1 ] == n - 1 ) break ; } }
Build the SA (sorted array of suffixes)
Build the SA ( sorted array of suffixes )
// Initialize SA (we're not storing full suffixes, only corresponding indices) // Initial sorting hint is the first char // Improve with exponential increments // Sort using current buckets // Update the buckets, prepare for the next iteration // Copy the buckets where they belong // If they are sorted (we have n buckets), break