Luogu P5108 looks up at the half moon night sky (suffix array)

Posted by wizardry on Sat, 30 Nov 2019 12:44:18 +0100

meaning of the title

Title Link

Sol

warning: the following is only 95 points. I took 1w + pictures and found no mistakes. I said that I could do nothing

We consider the ranking of each string to update the answer. Obviously, the prefix of suffix \ (1 \) must be the answer with the smallest dictionary order of the current length, but not necessarily the answer with the smallest left endpoint. Therefore, we need to use a data structure to maintain all feasible left endpoints. Then enumerate all the suffixes to update the answers.

#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 didn't write 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) {//height from pos, first position < 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;//The answer we're looking for 
    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;
}

Topics: C++