Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
- // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
- // #pragma comment(linker, "/stack:200000000"]
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <unordered_set>
- #include <unordered_map>
- #include <set>
- #include <map>
- #include <queue>
- #include <deque>
- #include <bitset>
- #include <stack>
- #include <random>
- #include <fstream>
- #include <sstream>
- #include <chrono>
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ld long double
- #define hm unordered_map
- #define pii pair<int, int>
- #define sz(a) (int)a.size()
- #define all(a) a.begin(), a.end()
- #define cinv(v) for (auto& x: v) cin >> x
- #define fr(i, n) for (int i = 0; i < n; ++i)
- #define fl(i, l, n) for (int i = l; i < n; ++i)
- // #define int ll
- template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
- template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
- using namespace std;
- #ifdef LOCAL
- #define dbg(x) cerr << #x << " : " << x << '\n'
- const int maxn = 20;
- #else
- #define dbg(x)
- const int maxn = 2e5 + 20;
- #endif
- //tg: @galebickosikasa
- ostream& operator << (ostream& out, vector<int>& v) {
- for (auto& x: v) out << x << ' ';
- return out;
- }
- ostream& operator << (ostream& out, pii& v) {
- out << v.fi << ", " << v.se;
- return out;
- }
- istream& operator >> (istream& in, pii& a) {
- in >> a.fi >> a.se;
- return in;
- }
- const ll inf = (ll) 2e9;
- const ld pi = asin (1) * 2;
- const ld eps = 1e-8;
- const ll mod = (ll)1e9 + 7;
- const ll ns = 97;
- mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
- void count_sort (vector<pair<pii, int>>& a) {
- int n = sz (a);
- vector<int> cnt (n);
- for (auto& x: a) ++cnt[x.fi.se];
- vector<pair<pii, int>> a_new (n);
- vector<int> pos (n);
- pos[0] = 0;
- fl (i, 1, n) pos[i] = pos[i - 1] + cnt[i - 1];
- for (auto& x: a) {
- int i = x.fi.se;
- a_new[pos[i]] = x;
- ++pos[i];
- }
- swap (a, a_new);
- cnt.assign (n, 0);
- pos.assign (n, 0);
- a_new.assign (n, pair<pii, int> ());
- for (auto& x: a) ++cnt[x.fi.fi];
- pos[0] = 0;
- fl (i, 1, n) pos[i] = pos[i - 1] + cnt[i - 1];
- for (auto& x: a) {
- int i = x.fi.fi;
- a_new[pos[i]] = x;
- ++pos[i];
- }
- swap (a, a_new);
- }
- vector<int> suffix_array (string s) {
- s += '$';
- int n = sz (s);
- vector<int> c (n);
- map <char, vector<int>> kek;
- fr (i, n) {
- kek[s[i]].pb (i);
- }
- int cls = 0;
- for (auto& v: kek) {
- for (auto& x: v.se) c[x] = cls;
- ++cls;
- }
- int len = 0;
- while ((1<<len) < n) {
- vector<pair<pii, int>> a;
- vector<int> _c (n);
- fr (i, n) a.pb ({{c[i], c[(i + (1<<len)) % n]}, i});
- // sort (all (a));
- count_sort (a);
- cls = -1;
- pii prev = {-1, -1};
- fr (i, sz (a)) {
- if (a[i].fi != prev) ++cls;
- _c[a[i].se] = cls;
- prev = a[i].fi;
- }
- swap (c, _c);
- ++len;
- }
- vector<int> res (n);
- fr (i, n) res[c[i]] = i;
- return res;
- }
- signed main () {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- string s;
- cin >> s;
- auto suff = suffix_array (s);
- cout << suff << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement