knakul853

Untitled

Jul 5th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.25 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/tree_policy.hpp>
  3. #include <ext/pb_ds/assoc_container.hpp>
  4. #include <ext/rope>
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7. using namespace __gnu_cxx;
  8.  
  9.      
  10. #define mod 1000000007
  11.  /*
  12.     /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ *** Debugging Start *** \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  13. */
  14. void __print(int x) {cerr << x;}
  15. void __print(long x) {cerr << x;}
  16. void __print(long long x) {cerr << x;}
  17. void __print(unsigned x) {cerr << x;}
  18. void __print(unsigned long x) {cerr << x;}
  19. void __print(unsigned long long x) {cerr << x;}
  20. void __print(float x) {cerr << x;}
  21. void __print(double x) {cerr << x;}
  22. void __print(long double x) {cerr << x;}
  23. void __print(char x) {cerr << '\'' << x << '\'';}
  24. void __print(const char *x) {cerr << '\"' << x << '\"';}
  25. void __print(const string &x) {cerr << '\"' << x << '\"';}
  26. void __print(bool x) {cerr << (x ? "true" : "false");}    
  27. template<typename T, typename V>
  28. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  29. template<typename T>
  30. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  31. void _print() {cerr << "]\n";}
  32. template <typename T, typename... V>
  33. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  34. #ifndef ONLINE_JUDGE
  35. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  36. #else
  37. #define debug(x...)
  38. #endif
  39. /*
  40.     /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ *** Debugging Ends *** \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  41. */
  42. using namespace __gnu_pbds;
  43. template <typename T>
  44. using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
  45.      
  46. /// Some frequent usable functions
  47. #define int                        long long
  48. void add(int &a,int b){a+=b;if(a>mod)a-=mod;}
  49. void sub(int &a,int b){a-=b;if(a<0)a+=mod;}
  50. void mul(int &a, int b) {a=1ll * a * b % mod;}
  51. template<typename T> T pow(T a,T b, long long m){T ans=1; while(b>0){ if(b%2==1) ans=(ans*a)%m; b/=2; a=(a*a)%m; } return ans%m; }
  52. int powmod(int a,int b)
  53. {int res = 1;while(b){if(b&1){res = (res * a)%mod;}b= b/2;a = (a*a)%mod;}return res;}
  54. int _ceil(int, int);
  55. int _floor(int a, int b) { return b < 0 ? _floor(-a, -b) : a < 0 ? -_ceil(-a, b) : a / b; }
  56. int _ceil(int a, int b) { return b < 0 ? _ceil(-a, -b) : a < 0 ? -_floor(-a, b) : (a + b - 1) / b; }
  57.      
  58. // int gcd(int a, int b, int &x, int &y) {if (a == 0) {x = 0; y = 1;return b;
  59. //     }int x1, y1;int d = gcd(b%a, a, x1, y1); x = y1 - (b / a) * x1;y = x1;return d;}
  60. // int find(int v){return v==parent[v]?v:parent[v] = find(parent[v]);}
  61. // void merge(int i,int j)
  62. //     {i = find(i);j = find(j);if(i == j)return;parent[parent[i]] = parent[j];cmp--;}
  63.      
  64. /*
  65.     /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ *** Directions in grids *** \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  66. */
  67. // int dy[4] = {0,0,1,-1}, dx[4] = {1,-1,0,0}; // 4 Direction
  68. // int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1};  // 8 Direction
  69. // int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1};  // Knight moves
  70. // int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1};  // Hexagonal Direction
  71. #pragma GCC target ("avx2")
  72. #pragma GCC optimization ("O3")
  73. #pragma GCC optimization ("unroll-loops")
  74. #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  75. #define sp(a , x) cout << fixed << setprecision(a) << x << endl;
  76. #define endl "\n"
  77. #define pb push_back
  78. #define pf push_front
  79. #define ub upper_bound
  80. #define lb lower_bound
  81. #define F first
  82. #define S second
  83. #define mset(a, b) memset(a, b, sizeof a)
  84. #define sz(x) ((int)(x.size()))
  85. #define sqr(x) ((x) * (x))
  86. #define graph vector<int>
  87. #define vi vector<int>
  88. #define vvi vector<vector<int>>
  89. #define pi pair<int,int>
  90. #define all(c)                      c.begin() , c.end()
  91. #define rep(i,a) for(int i=0;i<a;i++)
  92. #define rrep(i,a,b) for(int i=a;i>=b;i--)
  93. #define iter(it,a) for(auto it=a.begin();it!=a.end();it++)
  94. #define PQP priority_queue<pi, vector<pi>, greater<pi>>
  95. #define PQI priority_queue<int, vector<int>, greater<int>>
  96. #define dbg debug
  97. #define inf (int)1e16
  98. const long double EPS = 0.0000000001;
  99. const long double PI = 3.1415926535897932384;
  100. //vec.resize(unique(all(vec)) - vec.begin());
  101.  int dy[4] = {0,0,1,-1}, dx[4] = {1,-1,0,0};
  102.      
  103.   /// define some data.......
  104. const int N = (int)1e4+15;
  105.  
  106. vector<int> construct_suffix_array(const string& s)
  107. {
  108.    int n = sz(s);
  109.    vector<int>p(n, 0), c(n, 0);
  110.    const int limit =  257;
  111.    vector<int>cnt(limit, 0);
  112.  
  113.   // counting sort steps :
  114.    for(char c:s) cnt[c]++;
  115.    for(int i=1;i<limit;i++) cnt[i] += cnt[i-1];
  116.    for(int i=0;i<n;i++) p[ --cnt[s[i]] ] = i;
  117.  
  118.    // assign the classes.
  119.    c[p[0]] = 0;
  120.    int group = 1;
  121.    for(int i=1;i<n;i++)
  122.    {
  123.        if( s[p[i]] != s[p[i-1]] ) group++;
  124.  
  125.        c[p[i]] = group - 1;
  126.    }
  127.  
  128.    // after sorting the first elements we need to continue the iteration.
  129.    vector<int>pn(n, 0), cn(n, 0); // pn is for the second pair of the cyclic string.
  130.  
  131.  
  132.    for( int h = 0; (1LL<<h) < n; h++ )
  133.    {
  134.       for(int i=0;i<n;i++)
  135.       {
  136.          pn[i] = p[i] - (1LL<<h);
  137.  
  138.          if(pn[i] < 0) pn[i] += n;
  139.       }
  140.  
  141.       fill(cnt.begin(), cnt.end(), 0);
  142.      
  143.       // again using the counting sort technique other iterations.
  144.       for(int i=0; i<n; i++)
  145.       {
  146.           cnt[ c[ pn[ i ] ] ]++;
  147.       }
  148.       for(int i=1;i<limit;i++) cnt[i]+=cnt[i-1];
  149.  
  150.       for(int i=n-1;i>=0;i--)
  151.       {
  152.          p[--cnt[ c[ pn[ i ] ] ] ] = pn[i];
  153.       }
  154.  
  155.       cn[p[0]] = 0;
  156.       group = 1;
  157.       for(int i=1;i<n; i++)
  158.       {
  159.  
  160.          pair<int, int> cur = { c[ p[i] ], c[(p[i] + (1LL<<h)) % n] };
  161.          pair<int,int> pre = { c[ p[i-1] ], c[(p[i-1] + (1LL<<h)) % n] };
  162.  
  163.          if( cur != pre ) group++;
  164.  
  165.          cn[p[i]] = group - 1;
  166.       }
  167.  
  168.       swap(cn, c);
  169.  
  170.    }
  171.  
  172.    return p;
  173. }
  174.  
  175.  
  176. void solve()
  177. {
  178.   string s;
  179.   cin >> s;
  180.  
  181.   s += "$";
  182.  
  183.   vector<int>ans = construct_suffix_array(s);
  184.  
  185.    for(int c: ans)
  186.    {
  187.       cout<<c<<" ";
  188.    }
  189.  
  190. }    
  191. int32_t main()
  192.   {    
  193.      fast;
  194.      int test =1;
  195.     // cin >> test;
  196.      int tc=1;
  197.      while(test--)
  198.      {
  199.         solve();
  200.      
  201.  
  202.      }    
  203.      
  204.   }
Add Comment
Please, Sign In to add comment