模板
SA
ケロシの代码
const int N = 1e6 + 5;
int n, m;
string s;
int sa[N], rk[N], a[N], tp[N];
void Sort() {FOR(i, 1, m) a[i] = 0;FOR(i, 1, n) a[rk[i]] ++;FOR(i, 1, m) a[i] += a[i - 1];ROF(i, n, 1) sa[a[rk[tp[i]]] --] = tp[i];
}
void SA() {m = 130;FOR(i, 1, n) rk[i] = s[i];FOR(i, 1, n) tp[i] = i;Sort();for(int w = 1, p = 0; w <= n; m = p, p = 0, w <<= 1) {FOR(i, n - w + 1, n) tp[++ p] = i;FOR(i, 1, n) if(sa[i] > w) tp[++ p] = sa[i] - w;Sort(); swap(rk, tp);p = rk[sa[1]] = 1;FOR(i, 2, n) rk[sa[i]] = (tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + w] == tp[sa[i - 1] + w]) ? p : ++ p;if(p == n) return;}
}
void solve() {cin >> s; n = SZ(s); s = ' ' + s;SA();FOR(i, 1, n) cout << sa[i] << " ";cout << endl;
}