Madiyar

Untitled

Nov 13th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5. char a[100000];
  6. int cnt[100000],p[100000],col[100000],pn[100000],coln[100000];
  7.  
  8. int main()
  9. {
  10.     freopen("input.txt","r",stdin);
  11.     freopen("output.txt","w",stdout);
  12.              
  13.     gets(a);
  14.    
  15.     int n = strlen(a);
  16.     a[n++] = 'a'-1;
  17.    
  18.     for (int i = 0; i < n; ++i) {
  19.             a[i] -= 'a';
  20.             a[i]++;
  21.  
  22.         cnt[a[i]]++;
  23.     }
  24.  
  25.  
  26.     for (int i = 1; i < 27; ++i)
  27.         cnt[i] += cnt[i-1];
  28.  
  29.     for (int i = 0; i < n; ++i)
  30.         p[--cnt[a[i]]] = i;
  31.  
  32.     int cls = 0;
  33.     for (int i = 0; i < n; ++i) {
  34.         if (i > 0 && a[p[i-1]] != a[p[i]]) cls++;
  35.         col[p[i]] = cls;
  36.     }
  37.  
  38.     for (int h = 1; h < n; h<<=1) {
  39.  
  40.             memset(cnt,0,sizeof(cnt));
  41.  
  42.         for (int i = 0; i < n; ++i) {
  43.             pn[i] = p[i] - h;
  44.             if (pn[i] < 0) pn[i] += n;
  45.             cnt[col[pn[i]]]++;
  46.         }
  47.        
  48.         for (int i = 1; i <= cls; ++i)
  49.             cnt[i] += cnt[i-1];
  50.  
  51.         for (int i = n-1; i >= 0; --i)
  52.             p[--cnt[col[pn[i]]]] = pn[i];
  53.        
  54.         cls = 0;
  55.         for (int i = 0; i < n; ++i)
  56.         {
  57.             if (i > 0 && (col[p[i]] != col[p[i-1]] || col[(p[i]+h)%n] != col[(p[i-1]+h)%n])) cls++;
  58.             coln[p[i]] = cls;
  59.         }  
  60.  
  61.         memcpy(col,coln,sizeof(col));
  62.  
  63.     }
  64.  
  65.  
  66.     for (int i = 1; i < n; ++i)
  67.         printf("%d ",p[i]+1);
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment