题意
Sol
warning:下面这个做法只有95分,本地拍了1w+组都没找到错误我表示十分无能为力
我们考虑每个串的排名去更新答案,显然排名为(1)的后缀的前缀一定是当前长度的字典序最小的答案,但不一定是左端点最小的答案,因此还需要用一个数据结构去维护一下所有可行的左端点。然后枚举所有后缀更新答案就行了。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 10, SS = 6e5 + 10, INF = 1e9 + 10;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int CharSet, N, M, ans[SS];
int s[SS]; char stmp[SS];
int rak[SS], tp[SS], tax[MAXN * 10], sa[SS], height[SS];
void Sort() {
for(int i = 0; i <= M; i++) tax[i] = 0;
for(int i = 1; i <= N; i++) tax[rak[i]]++;
for(int i = 1; i <= M; i++) tax[i] += tax[i - 1];
//for(int i = N; i >= 1; i--) sa[i] = tp[tax[rak[i]]--];
for(int i = N; i >= 1; i--) sa[tax[rak[tp[i]]]--] = tp[i];
}
void SuffixArraryBuild() {
M = CharSet;
for(int i = 1; i <= N; i++) rak[i] = s[i], tp[i] = i;
Sort();
for(int w = 1, p; w < N; w <<= 1, M = p) {
p = 0;
for(int i = N - w + 1; i <= N; i++) tp[++p] = i;
for(int i = 1; i <= N; i++) if(sa[i] > w) tp[++p] = sa[i] - w;
Sort(); swap(rak, tp);
rak[sa[1]] = p = 1;
for(int i = 2; i <= N; i++)
rak[sa[i]] = (tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + w] == tp[sa[i - 1] + w] ? p : ++p);
}
for(int i = 1, k = 0; i <= N; i++) {
int j = sa[rak[i] - 1];
if(k) k--;
while(j && s[i + k] == s[j + k]) k++;//mmp一开始没写j..
height[rak[i]] = k;//tag
}
}
#define ls k << 1
#define rs k << 1 | 1
int mn[MAXN];
void update(int k) {
mn[k] = min(mn[ls], mn[rs]);
}
void Build(int k, int l, int r) {
if(l == r) {mn[k] = sa[l]; return ;}
int mid = l + r >> 1;
Build(ls, l, mid); Build(rs, mid + 1, r);
update(k);
}
int IntQuery(int k, int l, int r, int ql, int qr) {
if(ql <= l && r <= qr) return mn[k];
int mid = l + r >> 1;
if(ql > mid) return IntQuery(rs, mid + 1, r, ql, qr);
else if(qr <= mid) return IntQuery(ls, l, mid, ql, qr);
else return min(IntQuery(ls, l, mid, ql, qr), IntQuery(rs, mid + 1, r, ql, qr));
}
int f[MAXN][20];
int Find(int pos, int cur) {//从pos开始的height,第一个 < cur的位置
if(height[pos] < cur) return pos - 1;
for(int i = 19; i >= 1; i--) {
while(f[pos][i] >= cur && f[pos][i] <= INF && pos + (1 << i) - 1 <= N)
pos = pos + (1 << i) - 1;
}
return pos;
}
void solve() {
Build(1, 1, N);
memset(f, 0x3f, sizeof(f));
for(int i = 1; i <= N; i++) f[i][0] = height[i];
for(int j = 1; j <= 19; j++)
for(int i = 1; i + (1 << j) - 1<= N; i++)
chmin(f[i][j], min(f[i][j - 1], f[i + (1 << j - 1)][j - 1]));
int cur = 1;//马上要找的答案
for(int rk = 1; rk <= N; rk++) {
if((N - sa[rk] + 1) < cur) continue;
int pos = sa[rk];
for(int i = pos + cur - 1; i <= N; i++) {
int j = Find(rk + 1, cur);
ans[cur++] = IntQuery(1, 1, N, rk, j);
}
}
}
signed main() {
CharSet = read(); N = read();
if(CharSet == 26) {
scanf("%s", stmp + 1);
for(int i = 1; i <= N; i++) s[i] = stmp[i] - 'a';
}
else for(int i = 1; i <= N; i++) s[i] = read();
SuffixArraryBuild();
solve();
for(int i = 1; i <= N; i++) cout << ans[i] << ' ';
return 0;
}