提交记录 3424


用户 题目 状态 得分 用时 内存 语言 代码长度
owaski 1006. 【模板题】后缀排序 Wrong Answer 0 16.945 ms 31940 KB C++ 3.31 KB
提交时间 评测时间
2018-07-14 23:31:46 2020-07-31 21:17:14
#include <cstdio>
#include <cstring>
#include <algorithm>
#define debug(...) fprintf(stderr, __VA_ARGS__)

int OutN;
char Out[10000000];
int tmpOutN;
char tmpOut[20];

template<class T>
inline void write(T x) {
    if(x < 0) x = -x, Out[OutN++] = '-';
    if(x) {
        tmpOutN = 0;
        while(x) {
            tmpOut[tmpOutN++] = x%10+'0';
            x /= 10;
        }
        while(tmpOutN--) Out[OutN++] = tmpOut[tmpOutN];
    }
    else Out[OutN++] = '0';
}

const int N = 200009;
const int SIGMA = 26;

int n;
char str[N];
int sa[N], tot;
int rk[N], h[N];

struct Edge {
    int u, v, c;

    Edge() {u = v = 0;}
    Edge(int _u, int _v, int _c) {u = _u, v = _v, c = _c;}
}e[N];
int eN;

struct Tree {
    int sz, head[N], to[N], ne[N];

    Tree() {
        sz = 1;
        memset(head, 0, sizeof head);
    }

    inline void addEdge(int u, int v) {
        to[sz] = v, ne[sz] = head[u], head[u] = sz++;
    }
}T;

struct SAM {
    int root, sz, last, fa[N], maxl[N], right[N], ne[N][SIGMA], pos[N];
    
    SAM() {
        sz = root = last = 1, fa[1] = maxl[1] = 0;
        memset(ne[1], 0, sizeof ne[1]);
    }
    
    inline int newNode(int l) {
        maxl[++sz] = l, fa[sz] = 0;
        memset(ne[sz], 0, sizeof ne[sz]);
        return sz;
    }
    
    inline void add(int x, int i) {
        int p = last, np = newNode(maxl[p]+1);
        while(p && !ne[p][x])
            ne[p][x] = np, p = fa[p];
        if(!p) fa[np] = root;
        else {
            int q = ne[p][x];
            if(maxl[q] == maxl[p]+1) fa[np] = q;
            else {
                int r = newNode(maxl[p]+1);
                memcpy(ne[r], ne[q], sizeof ne[r]);
                fa[r] = fa[q], right[r] = right[q], fa[q] = fa[np] = r;
                while(p && ne[p][x] == q)
                    ne[p][x] = r, p = fa[p];
            }
        }
        right[np] = maxl[np], pos[np] = i, last = np;
    }

    inline int get(int x, int y) {
        return str[n-right[y]+1+maxl[x]]-'a';
    }
    
    inline void construct() {
        static int cnt[SIGMA], top[N];
        for(int i = 2; i <= sz; ++i) {
            e[++eN] = Edge(fa[i], i, get(fa[i], i));
            cnt[e[eN].c]++;
        }
        for(int i = 1; i < SIGMA; ++i) cnt[i] += cnt[i-1];
        for(int i = 1; i <= eN; ++i) top[cnt[e[i].c]--] = i;
        for(int i = eN; i >= 1; --i) T.addEdge(e[top[i]].u, e[top[i]].v);
    }

    void dfs(int now) {
        if(pos[now]) sa[++tot] = pos[now];
        for(int i = T.head[now]; i; i = T.ne[i])
            dfs(T.to[i]);
    }
}sam;


inline void init() {
    scanf("%s", str+1);
}

inline void solve() {
    n = strlen(str+1);
    for(int i = n; i >= 1; --i)
        sam.add(str[i]-'a', i);
    sam.construct(), sam.dfs(1);
    for(int i = 1; i <= n; ++i) rk[sa[i]] = i;
    for(int i = 1, k = 0; i <= n; ++i) {
        if(k) k--;
        int j = sa[rk[i]-1];
        while(str[i+k] == str[j+k]) k++;
        h[rk[i]] = k;
    }
    for(int i = 1; i <= n; ++i)
        write(sa[i]), Out[OutN++] = i==n?'\n':' ';
    for(int i = 2; i <= n; ++i)
        write(h[i]), Out[OutN++] = i==n?'\n':' ';
    fwrite(Out, sizeof(char), OutN, stdout);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    init();
    solve();
    
#ifndef ONLINE_JUDGE
    fclose(stdin);fclose(stdout);
#endif
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #1395.66 us3 MB + 124 KBWrong AnswerScore: -100

Subtask #1 Testcase #2403.42 us3 MB + 124 KBAcceptedScore: 100

Subtask #1 Testcase #3400.12 us3 MB + 124 KBAcceptedScore: 0

Subtask #1 Testcase #4403.46 us3 MB + 124 KBAcceptedScore: 0

Subtask #1 Testcase #5402.31 us3 MB + 124 KBAcceptedScore: 0

Subtask #1 Testcase #6401.49 us3 MB + 124 KBAcceptedScore: 0

Subtask #1 Testcase #711.603 ms21 MB + 920 KBAcceptedScore: 0

Subtask #1 Testcase #815.444 ms27 MB + 64 KBAcceptedScore: 0

Subtask #1 Testcase #916.945 ms27 MB + 852 KBAcceptedScore: 0

Subtask #1 Testcase #1010.018 ms19 MB + 312 KBAcceptedScore: 0

Subtask #1 Testcase #1110.772 ms21 MB + 544 KBAcceptedScore: 0

Subtask #1 Testcase #125.892 ms22 MB + 252 KBAcceptedScore: 0

Subtask #1 Testcase #138.506 ms27 MB + 888 KBAcceptedScore: 0

Subtask #1 Testcase #1411.268 ms23 MB + 536 KBAcceptedScore: 0

Subtask #1 Testcase #1515.767 ms31 MB + 196 KBAcceptedScore: 0

Subtask #1 Testcase #165.794 ms20 MB + 736 KBAcceptedScore: 0

Subtask #1 Testcase #175.781 ms19 MB + 824 KBAcceptedScore: 0

Subtask #1 Testcase #185.816 ms19 MB + 644 KBAcceptedScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2026-04-18 16:12:41 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠