提交记录 3422


用户 题目 状态 得分 用时 内存 语言 代码长度
owaski 1006. 【模板题】后缀排序 Wrong Answer 0 16.975 ms 31960 KB C++ 3.33 KB
提交时间 评测时间
2018-07-14 23:30:24 2020-07-31 21:17:10
#include <cstdio>
#include <cstring>
#include <iostream>
#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 #1428.28 us3 MB + 144 KBWrong AnswerScore: -100

Subtask #1 Testcase #2436.6 us3 MB + 144 KBAcceptedScore: 100

Subtask #1 Testcase #3425.64 us3 MB + 144 KBAcceptedScore: 0

Subtask #1 Testcase #4428.06 us3 MB + 144 KBAcceptedScore: 0

Subtask #1 Testcase #5430.32 us3 MB + 144 KBAcceptedScore: 0

Subtask #1 Testcase #6425.61 us3 MB + 144 KBAcceptedScore: 0

Subtask #1 Testcase #711.658 ms21 MB + 952 KBAcceptedScore: 0

Subtask #1 Testcase #815.536 ms27 MB + 88 KBAcceptedScore: 0

Subtask #1 Testcase #916.975 ms27 MB + 872 KBAcceptedScore: 0

Subtask #1 Testcase #1010.104 ms19 MB + 332 KBAcceptedScore: 0

Subtask #1 Testcase #1110.806 ms21 MB + 564 KBAcceptedScore: 0

Subtask #1 Testcase #125.929 ms22 MB + 272 KBAcceptedScore: 0

Subtask #1 Testcase #138.534 ms27 MB + 908 KBAcceptedScore: 0

Subtask #1 Testcase #1411.375 ms23 MB + 560 KBAcceptedScore: 0

Subtask #1 Testcase #1515.887 ms31 MB + 216 KBAcceptedScore: 0

Subtask #1 Testcase #165.8 ms20 MB + 756 KBAcceptedScore: 0

Subtask #1 Testcase #175.805 ms19 MB + 844 KBAcceptedScore: 0

Subtask #1 Testcase #185.829 ms19 MB + 664 KBAcceptedScore: 0


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