Advertisement
Galebickosikasa

Untitled

Nov 21st, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
  2. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
  3. // #pragma comment(linker, "/stack:200000000"]
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <unordered_set>
  10. #include <unordered_map>
  11. #include <set>
  12. #include <map>
  13. #include <queue>
  14. #include <deque>
  15. #include <bitset>
  16. #include <stack>
  17. #include <random>
  18. #include <fstream>
  19. #include <sstream>
  20. #include <chrono>
  21.  
  22. #define fi first
  23. #define se second
  24. #define pb push_back
  25. #define ll long long
  26. #define ld long double
  27. #define hm unordered_map
  28. #define pii pair<int, int>
  29. #define sz(a) (int)a.size()
  30. #define all(a) a.begin(), a.end()
  31. #define cinv(v) for (auto& x: v) cin >> x
  32. #define fr(i, n) for (int i = 0; i < n; ++i)
  33. #define fl(i, l, n) for (int i = l; i < n; ++i)
  34.  
  35. // #define int ll
  36.  
  37. template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
  38. template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
  39.  
  40. using namespace std;
  41.  
  42. #ifdef LOCAL
  43. #define dbg(x) cerr << #x << " : " << x << '\n'
  44. const int maxn = 20;
  45. #else
  46. #define dbg(x)
  47. const int maxn = 2e5 + 20;
  48. #endif
  49.  
  50. //tg: @galebickosikasa
  51.  
  52. ostream& operator << (ostream& out, vector<int>& v) {
  53. for (auto& x: v) out << x << ' ';
  54. return out;
  55. }
  56.  
  57. ostream& operator << (ostream& out, pii& v) {
  58. out << v.fi << ", " << v.se;
  59. return out;
  60. }
  61.  
  62. istream& operator >> (istream& in, pii& a) {
  63. in >> a.fi >> a.se;
  64. return in;
  65. }
  66.  
  67. const ll inf = (ll) 2e9;
  68. const ld pi = asin (1) * 2;
  69. const ld eps = 1e-8;
  70. const ll mod = (ll)1e9 + 7;
  71. const ll ns = 97;
  72.  
  73. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  74.  
  75. void count_sort (vector<pair<pii, int>>& a) {
  76. int n = sz (a);
  77. vector<int> cnt (n);
  78. for (auto& x: a) ++cnt[x.fi.se];
  79. vector<pair<pii, int>> a_new (n);
  80. vector<int> pos (n);
  81. pos[0] = 0;
  82. fl (i, 1, n) pos[i] = pos[i - 1] + cnt[i - 1];
  83. for (auto& x: a) {
  84. int i = x.fi.se;
  85. a_new[pos[i]] = x;
  86. ++pos[i];
  87. }
  88. swap (a, a_new);
  89. cnt.assign (n, 0);
  90. pos.assign (n, 0);
  91. a_new.assign (n, pair<pii, int> ());
  92. for (auto& x: a) ++cnt[x.fi.fi];
  93. pos[0] = 0;
  94. fl (i, 1, n) pos[i] = pos[i - 1] + cnt[i - 1];
  95. for (auto& x: a) {
  96. int i = x.fi.fi;
  97. a_new[pos[i]] = x;
  98. ++pos[i];
  99. }
  100. swap (a, a_new);
  101. }
  102.  
  103. vector<int> suffix_array (string s) {
  104. s += '$';
  105. int n = sz (s);
  106. vector<int> c (n);
  107. map <char, vector<int>> kek;
  108. fr (i, n) {
  109. kek[s[i]].pb (i);
  110. }
  111. int cls = 0;
  112. vector<pair<pii, int>> a (n);
  113. for (auto& v: kek) {
  114. for (auto& x: v.se) c[x] = cls;
  115. ++cls;
  116. }
  117. int len = 0;
  118. while ((1<<len) < n) {
  119. vector<pair<pii, int>> _a (n);
  120. vector<int> _c (n);
  121. fr (i, n) a.pb ({{c[i], c[(i + (1<<len)) % n]}, i});
  122. count_sort (a);
  123. cls = -1;
  124. pii prev = {-1, -1};
  125. fr (i, sz (a)) {
  126. if (a[i].fi != prev) ++cls;
  127. _c[a[i].se] = cls;
  128. prev = a[i].fi;
  129. }
  130. swap (c, _c);
  131. ++len;
  132. }
  133. vector<int> res (n);
  134. fr (i, n) res[c[i]] = i;
  135. return res;
  136. }
  137.  
  138. signed main () {
  139. ios_base::sync_with_stdio(false);
  140. cin.tie(nullptr);
  141. cout.tie(nullptr);
  142. string s;
  143. cin >> s;
  144. auto suff = suffix_array (s);
  145. cout << suff << '\n';
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement