06. Sorting/kasai_algorithm.cpp
vector<int> buildSuffixArray(string txt, int n)
{
    // A structure to store suffixes and their indexes
    struct suffix suffixes[n];
 
    // Store suffixes and their indexes in an array of structures.
    // The structure is needed to sort the suffixes alphabatically
    // and maintain their old indexes while sorting
    for (int i = 0; i < n; i++)
    {
        suffixes[i].index = i;
        suffixes[i].rank[0] = txt[i] - 'a';
        suffixes[i].rank[1] = ((i+1) < n)? (txt[i + 1] - 'a'): -1;
    }
 
    // Sort the suffixes using the comparison function
    // defined above.
    sort(suffixes, suffixes+n, cmp);
 
    // At his point, all suffixes are sorted according to first
    // 2 characters.  Let us sort suffixes according to first 4
    // characters, then first 8 and so on
    int ind[n];  // This array is needed to get the index in suffixes[]
    // from original index.  This mapping is needed to get
    // next suffix.
    for (int k = 4; k < 2*n; k = k*2)
    {
        // Assigning rank and index values to first suffix
        int rank = 0;
        int prev_rank = suffixes[0].rank[0];
        suffixes[0].rank[0] = rank;
        ind[suffixes[0].index] = 0;
 
        // Assigning rank to suffixes
        for (int i = 1; i < n; i++)
        {
            // If first rank and next ranks are same as that of previous
            // suffix in array, assign the same new rank to this suffix
            if (suffixes[i].rank[0] == prev_rank &&
                    suffixes[i].rank[1] == suffixes[i-1].rank[1])
            {
                prev_rank = suffixes[i].rank[0];
                suffixes[i].rank[0] = rank;
            }
            else // Otherwise increment rank and assign
            {
                prev_rank = suffixes[i].rank[0];
                suffixes[i].rank[0] = ++rank;
            }
            ind[suffixes[i].index] = i;
        }
 
        // Assign next rank to every suffix
        for (int i = 0; i < n; i++)
        {
            int nextindex = suffixes[i].index + k/2;
            suffixes[i].rank[1] = (nextindex < n)?
                                  suffixes[ind[nextindex]].rank[0]: -1;
        }
 
        // Sort the suffixes according to first k characters
        sort(suffixes, suffixes+n, cmp);
    }
 
    // Store indexes of all sorted suffixes in the suffix array
    vector<int>suffixArr;
    for (int i = 0; i < n; i++)
        suffixArr.push_back(suffixes[i].index);
 
    // Return the suffix array
    return  suffixArr;
}
// This is the main function that takes a string 'txt' of size n as an
// argument, builds and return the suffix array for the given string
This is the main function that takes a string 'txt' of size n as an
argument, builds and return the suffix array for the given string
This is the main function that takes a string ' txt ' of size n as an argument builds and return the suffix array for the given string
vector<int> buildSuffixArray(string txt, int n)
{
    struct suffix suffixes[n];
    for (int i = 0; i < n; i++)
    {
        suffixes[i].index = i;
        suffixes[i].rank[0] = txt[i] - 'a';
        suffixes[i].rank[1] = ((i+1) < n)? (txt[i + 1] - 'a'): -1;
    }
    sort(suffixes, suffixes+n, cmp);
    int ind[n];  
    for (int k = 4; k < 2*n; k = k*2)
    {
        int rank = 0;
        int prev_rank = suffixes[0].rank[0];
        suffixes[0].rank[0] = rank;
        ind[suffixes[0].index] = 0;
        for (int i = 1; i < n; i++)
        {
            if (suffixes[i].rank[0] == prev_rank &&
                    suffixes[i].rank[1] == suffixes[i-1].rank[1])
            {
                prev_rank = suffixes[i].rank[0];
                suffixes[i].rank[0] = rank;
            }
            else 
            {
                prev_rank = suffixes[i].rank[0];
                suffixes[i].rank[0] = ++rank;
            }
            ind[suffixes[i].index] = i;
        }
        for (int i = 0; i < n; i++)
        {
            int nextindex = suffixes[i].index + k/2;
            suffixes[i].rank[1] = (nextindex < n)?
                                  suffixes[ind[nextindex]].rank[0]: -1;
        }
        sort(suffixes, suffixes+n, cmp);
    }
    vector<int>suffixArr;
    for (int i = 0; i < n; i++)
        suffixArr.push_back(suffixes[i].index);
    return  suffixArr;
}
vector < int > buildSuffixArray ( string txt , int n ) { struct suffix suffixes [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { suffixes [ i ] . index = i ; suffixes [ i ] . rank [ 0 ] = txt [ i ] - ' ' ; suffixes [ i ] . rank [ 1 ] = ( ( i + 1 ) < n ) ? ( txt [ i + 1 ] - ' ' ) : -1 ; } sort ( suffixes , suffixes + n , cmp ) ; int ind [ n ] ; for ( int k = 4 ; k < 2 * n ; k = k * 2 ) { int rank = 0 ; int prev_rank = suffixes [ 0 ] . rank [ 0 ] ; suffixes [ 0 ] . rank [ 0 ] = rank ; ind [ suffixes [ 0 ] . index ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( suffixes [ i ] . rank [ 0 ] == prev_rank && suffixes [ i ] . rank [ 1 ] == suffixes [ i - 1 ] . rank [ 1 ] ) { prev_rank = suffixes [ i ] . rank [ 0 ] ; suffixes [ i ] . rank [ 0 ] = rank ; } else { prev_rank = suffixes [ i ] . rank [ 0 ] ; suffixes [ i ] . rank [ 0 ] = ++ rank ; } ind [ suffixes [ i ] . index ] = i ; } for ( int i = 0 ; i < n ; i ++ ) { int nextindex = suffixes [ i ] . index + k / 2 ; suffixes [ i ] . rank [ 1 ] = ( nextindex < n ) ? suffixes [ ind [ nextindex ] ] . rank [ 0 ] : -1 ; } sort ( suffixes , suffixes + n , cmp ) ; } vector < int > suffixArr ; for ( int i = 0 ; i < n ; i ++ ) suffixArr . push_back ( suffixes [ i ] . index ) ; return suffixArr ; }
This is the main function that takes a string 'txt' of size n as an
argument, builds and return the suffix array for the given string
This is the main function that takes a string ' txt ' of size n as an argument builds and return the suffix array for the given string
// A structure to store suffixes and their indexes // Store suffixes and their indexes in an array of structures. // The structure is needed to sort the suffixes alphabatically // and maintain their old indexes while sorting // Sort the suffixes using the comparison function // defined above. // At his point, all suffixes are sorted according to first // 2 characters.  Let us sort suffixes according to first 4 // characters, then first 8 and so on // This array is needed to get the index in suffixes[] // from original index.  This mapping is needed to get // next suffix. // Assigning rank and index values to first suffix // Assigning rank to suffixes // If first rank and next ranks are same as that of previous // suffix in array, assign the same new rank to this suffix // Otherwise increment rank and assign // Assign next rank to every suffix // Sort the suffixes according to first k characters // Store indexes of all sorted suffixes in the suffix array // Return the suffix array