Advertisement
GerONSo

SuffArr

Jan 7th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. /*
  2. ┓┏┓┏┓┃
  3. ┛┗┛┗┛┃
  4. ┓┏┓┏┓┃
  5. ┛┗┛┗┛┃
  6. ┓┏┓┏┓┃\○/
  7. ┛┗┛┗┛┃ / /
  8. ┓┏┓┏┓┃ノ
  9. ┛┗┛┗┛┃
  10. ┓┏┓┏┓┃
  11. ┛┗┛┗┛┃
  12. ┓┏┓┏┓┃
  13. ┛┗┛┗┛┃
  14. ┓┏┓┏┓┃
  15. ┛┗┛┗┛┃
  16. ┓┏┓┏┓┃┓
  17. ┛┗┛┗┛┃┃
  18. MIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSIC
  19. */
  20.  
  21. // #define pragma
  22.  
  23. #ifdef pragma
  24. #pragma GCC optimize("Ofast")
  25. #pragma GCC optimize("no-stack-protector")
  26. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  27. #pragma GCC optimize("unroll-loops")
  28. #pragma GCC diagnostic ignored "-Wunused-result"
  29. #endif // pragma
  30.  
  31. #include<bits/stdc++.h>
  32. #include <ext/pb_ds/assoc_container.hpp>
  33. #include <ext/pb_ds/tree_policy.hpp>
  34.  
  35. #define ll long long
  36. #define all(x) begin(x), end(x)
  37. #define pb push_back
  38. #define x first
  39. #define y second
  40. #define int long long
  41. #define zero(two) memset(two, 0, sizeof(two))
  42.  
  43. using namespace std;
  44. using namespace __gnu_pbds;
  45.  
  46.  
  47. typedef vector<int> vi;
  48. typedef vector<bool> vb;
  49. typedef pair<int, int> pii;
  50. typedef long double ld;
  51. typedef vector<vi> matrix;
  52. template<typename T>
  53. using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  54.  
  55. const ld PI = atan2(0, -1);
  56.  
  57. void seriy() {
  58. ios::sync_with_stdio(0);
  59. cin.tie(0);
  60. cout.tie(0);
  61. cout << fixed << setprecision(10);
  62. #if 1
  63. freopen("input", "r", stdin);
  64. freopen("output", "w", stdout);
  65. #endif
  66. }
  67.  
  68. const int MAXN = 1e6 + 10;
  69.  
  70. vi p(MAXN), cnt(MAXN), c(MAXN);
  71. vi pn(MAXN), cn(MAXN);
  72.  
  73. signed main() {
  74. seriy();
  75. string t, s;
  76. cin >> t >> s;
  77. int n = s.size();
  78. int m = t.size();
  79. for(int i = 0; i < n; i++) {
  80. cnt[s[i]]++;
  81. }
  82. for(int i = 1; i < MAXN; i++) {
  83. cnt[i] += cnt[i - 1];
  84. }
  85. for(int i = 0; i < n; i++) {
  86. p[--cnt[s[i]]] = i;
  87. }
  88. c[p[0]] = 0;
  89. int cur = 1;
  90. for(int i = 1; i < n; i++) {
  91. if (s[p[i]] != s[p[i - 1]]) {
  92. ++cur;
  93. }
  94. c[p[i]] = cur - 1;
  95. }
  96.  
  97. for(int h = 0; (1 << h) < n; ++h) {
  98. for(int i = 0; i < n; i++) {
  99. pn[i] = (p[i] + n - (1 << h)) % n;
  100. }
  101. cnt.assign(MAXN, 0);
  102. for(int i = 0; i < n; i++) {
  103. cnt[c[pn[i]]]++;
  104. }
  105. for(int i = 1; i < cur; i++) {
  106. cnt[i] += cnt[i - 1];
  107. }
  108. for(int i = n - 1; i >= 0; i--) {
  109. p[--cnt[c[pn[i]]]] = pn[i];
  110. }
  111. cn[p[0]] = 0;
  112. cur = 1;
  113. for(int i = 1; i < n; i++) {
  114. int m1 = (p[i] + (1 << h)) % n, m2 = (p[i - 1] + (1 << h)) % n;
  115. if (c[p[i]] != c[p[i - 1]] || c[m1] != c[m2]) {
  116. cur++;
  117. }
  118. cn[p[i]] = cur - 1;
  119. }
  120. c = cn;
  121. }
  122. for(int i = 0; i < n; i++) {
  123. cerr << p[i] << " ";
  124. }
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement