voidcounting_sort(){ memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= n; ++i) ++cnt[a[i]]; for (int i = 1; i <= w; ++i) cnt[i] += cnt[i - 1]; for (int i = n; i >= 1; --i) b[cnt[a[i]]--] = a[i]; }
#include<bits/stdc++.h> usingnamespace std; intmain(){ string s; cin >> s; int n = s.size(); s = '$' + s; int m = 127; vector<int> sa(n + 1), rk((n + 1) << 1), cnt(m + 1); for (int i = 1; i <= n; ++i) ++cnt[rk[i] = s[i]]; for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1]; for (int i = n; i >= 1; --i) sa[cnt[rk[i]]--] = i; auto _rk = rk; for (int p = 0, i = 1; i <= n; ++i) if (_rk[sa[i]] == _rk[sa[i - 1]]) rk[sa[i]] = p; else rk[sa[i]] = ++p; m = n; cnt.resize(m + 1); for (int w = 1; w < n; w <<= 1) { fill(cnt.begin(), cnt.end(), 0); auto _sa = sa; for (int i = 1; i <= n; ++i) ++cnt[rk[_sa[i] + w]]; for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1]; for (int i = n; i >= 1; --i) sa[cnt[rk[_sa[i] + w]]--] = _sa[i];
fill(cnt.begin(), cnt.end(), 0); _sa = sa; for (int i = 1; i <= n; ++i) ++cnt[rk[_sa[i]]]; for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1]; for (int i = n; i >= 1; --i) sa[cnt[rk[_sa[i]]]--] = _sa[i]; _rk = rk; for (int p = 0, i = 1; i <= n; ++i) if (_rk[sa[i]] == _rk[sa[i - 1]] && _rk[sa[i] + w] == _rk[sa[i - 1] + w]) rk[sa[i]] = p; else rk[sa[i]] = ++p; } for (int i = 1; i <= n; ++i) cout << sa[i] << " \n"[i == n]; return0; }