Advertisement
Guest User

Untitled

a guest
Jun 6th, 2015
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<limits.h>
  4. #include<math.h>
  5.  
  6. using namespace std;
  7.  
  8. int MAXN;
  9. int MAXLG;
  10.  
  11. struct node
  12. {
  13.     int rank[2];
  14.     int index;
  15. };
  16.  
  17.  
  18. /*Sort the L struct. If rank of current half is same, the sort by rank of next half and if it's not same then sort by the current half itself.*/
  19. int cmp(struct node a,struct node b)
  20. {
  21.     return a.rank[0] == b.rank[0] ? (a.rank[1] < b.rank[1] ? 1 : 0) : (a.rank[0] < b.rank[0]) ? 1 :0;
  22. }
  23.  
  24. int main()
  25. {
  26.     printf("Enter the string\n");
  27.     char c;
  28.     char *s = new char[100001];
  29.     int i = 0;
  30.     while((c = getchar())!='\n')
  31.     {
  32.         s[i++] = c;
  33.     }
  34.     s[i] = '\0';
  35.  
  36.     /*
  37.     *
  38.     Important : MAXN is length of string which will serve as column of the P array and MAXLG will serve as the row of the P array. in first row of P array,
  39.     we will be storing the rank of suffixes in 's' starting at position 'j' (some column). In the second row of P array, we will be storing the rank of suffix
  40.     starting at position 'j' and 2^i elements compared. 'i' being the index of row of P array
  41.     */
  42.  
  43.     MAXN = i;
  44.     MAXLG = log(MAXN)/log(2) + 1;
  45.            
  46.     int P[MAXLG][MAXN];
  47.    
  48.     /* In the first row of P-matrix, we will be storing the rank of suffix starting from 'j' and total 2^i elements (i = 0) for first row.*/
  49.        
  50.  
  51.     /*This struct will store 3 entries. rank of the current half of the suffix, rank of the next half of the suffix and the index of suffix.*/
  52.     struct node L[MAXN];
  53.  
  54.     for(int i=0;i<MAXN;i++)
  55.         P[0][i] = s[i]-'a';
  56.  
  57.     /*Step will fill the P-matrix and count will have count of upto how many elements we have compared. At every increment, we will double the count */
  58.     int step = 1;
  59.     int count = 1;
  60.  
  61.     for(count=1,step = 1;count < MAXN; count *= 2,step++)
  62.     {
  63.         for(int i=0;i<MAXN;i++)
  64.         {
  65.             /*This will store the rank of the current half.*/
  66.             L[i].rank[0] = P[step-1][i];
  67.             /*This will store the rank of the next half*/
  68.             L[i].rank[1] = i + count < MAXN ? P[step-1][i+count] : -1;
  69.             /*This will store the index of the suffix*/
  70.             L[i].index = i;        
  71.         }
  72.         sort(L,L+MAXN,cmp);
  73.  
  74.         /*This loop will set the rank of suffixes. upto length 2^count*/
  75.         for(int i=0;i<MAXN;i++)
  76.         {
  77.             /* if both ranks of L[i+1] is same, then P[step][L[i].index] = P[step][L[i-1].index], else the rank will be i.*/
  78.             if(i == 0)
  79.             {
  80.                 P[step][L[i].index] = 0;
  81.             }
  82.             else
  83.             {
  84.                 if(L[i].rank[0] == L[i-1].rank[0] && L[i].rank[1] == L[i-1].rank[1])
  85.                 {
  86.                     P[step][L[i].index] = P[step][L[i-1].index];
  87.                 }
  88.                 else
  89.                 {
  90.                     P[step][L[i].index] = i;
  91.                 }
  92.             }
  93.         }
  94.         /*for(int i=0;i<MAXLG;i++)
  95.         {
  96.             for(int j=0;j<MAXN;j++)
  97.             {
  98.                 printf("%d ",P[i][j]);
  99.             }
  100.             printf("\n");
  101.         }*/
  102.     }
  103.     for(int i=0;i<MAXN;i++)
  104.     {
  105.         printf("%d ",P[MAXLG-1][i]);
  106.     }
  107.     printf("\n");
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement