Suffix array Hunzi brush title record


Luo Gu P3809 [template] suffix sorting

  • First to one of the most water template title o (* ≧ ▽ ≦) tsu ┏━┓
  • SA [Nmax]   SA [i] ranking as the number of subscript i is the number
  • the X-[Nmax]     the X-[i] value, value, each cycle should be based on the updated rankings again
  • Y [Nmax of]     Y [i] as an intermediate array using twice, first SA [i] is updated according to the position of the second keyword ranking number i of the first key, the second update X [i] (since Comparative used when the original value x [i], it first with y [i] to update, the last y [i] is assigned to x [i])
  • qsort () function known per second from the keyword ranking (y [i]) and a first key value (x [i]) find the rank number, using a radix sort result in sa [i] inside
  • After each cycle state: x [i], sa [i] has been determined
  • The outermost loop l: current corresponding to sa [i] of the l, after this cycle sa [i] refer to the length becomes 2l
  • So l <n (or <= n / 2) 
  • With great difficulty writing bad either suffix array. . . Code is wrong some easy points
  • Code:
    . 1 #include <bits / STDC ++ H.>
     2  #define Nmax of 1000010
     . 3  
    . 4  the using  namespace STD;
     . 5  int C [Nmax of], RK [Nmax of], SA [Nmax of], X [Nmax of], Y [Nmax of];
     . 6  / / Y [i] a second keyword ranking number i of the position of the first keyword 
    . 7  
    . 8  char S [Nmax of];
     . 9  int n-, K, m = 200 is ;
     10  
    . 11  void qsort () {
     12 is      for ( int i = . 1 ; I <= m; I ++) C [I] = 0 ;
     13 is      for ( int I = . 1; i<=n; i++) c[ x[i] ]++;
    14     for (int i=1; i<=m; i++) c[i]+=c[i-1];
    15     for (int i=n; i>=1; i--) sa[ c[ x[y[i]] ]-- ]=y[i];
    16 }
    17 
    18 int main(){
    19     scanf("%s",s+1);
    20     n=strlen(s+1);
    21     for (int i=1; i<=n; i++) x[i]=s[i];
    22     for (int I = . 1 ; I <= n-; I ++) Y [I] = I;
     23 is      qsort ();
     24      for ( int L = . 1 ; L <n-; L << = . 1 ) { 
     25          int T = 0 ;
     26 is          for ( int I = n-L-+ . 1 ; I <= n-; I ++) Y [T ++] = I; 
     27          // some figures are not the first key, the second key is not sa some numbers [i ] can cover the range of 
    28          for ( int I = . 1 ; I <= n-; I ++) IF (SA [I]> L) Y [T ++] = SA [I] -l;   // if there is a first keywords 
    29          qsort ();
     30         m = . 1 ;
     31 is          Y [SA [ . 1 ]] = m;
     32          for ( int I = 2 ; I <= n-; I ++ ) {
     33 is              // fallibility Here, sa [i] refer to the lengths 2l, but x refers to the length of only l, so also the second keyword comparison 
    34 is              IF (x [SA [I]] == x [SA [I- . 1 ]]) IF (x [SA [I] + L] = X = [SA [I- . 1 ] + L]) {Y [SA [I]] = m; Continue ;}
     35              Y [SA [I]] ++ = m;
     36          }
     37 [          for ( int I = . 1 ; I <= n-; I ++) X [I] = Y [I];
     38 is     }
    39     for (int i=1; i<=n; i++) printf("%d ",sa[i]);
    40     return 0;
    41 }
    (<ゝ ω ·) ☆

     

Guess you like

Origin www.cnblogs.com/jiecaoer/p/11609119.html