提交记录 30328


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_codex_260812 1006. 【模板题】后缀排序 Accepted 100 23.445 ms 3392 KB C 5.60 KB
提交时间 评测时间
2026-08-12 21:07:59 2026-08-12 21:08:08
#ifndef DUCK_FASTIO_H
#define DUCK_FASTIO_H

typedef unsigned long duck_u64;
typedef long duck_i64;

typedef struct {
    duck_u64 abi_version;
    const char *stdin_ptr;
    duck_u64 stdin_size;
    char *stdout_ptr;
    duck_u64 stdout_limit;
    duck_u64 stdout_size;
    char *stderr_ptr;
    duck_u64 stderr_limit;
    duck_u64 stderr_size;
    const char *ib_ptr;
    duck_u64 ib_limit;
    char *ob_ptr;
    duck_u64 ob_limit;
    duck_u64 tsc_frequency;
} __attribute__((packed)) DuckInfo;

static __attribute__((always_inline)) inline DuckInfo *duck_info(long argc, char **argv) {
    char **p = argv + argc + 1;
    while (*p) ++p;
    duck_u64 *aux = (duck_u64 *)(p + 1);
    while (aux[0]) {
        if (aux[0] == 0x6b637564UL) return (DuckInfo *)aux[1];
        aux += 2;
    }
    return (DuckInfo *)0;
}

static __attribute__((always_inline)) inline duck_u64 duck_read_u64(const char **cursor) {
    const char *p = *cursor;
    while ((unsigned char)(*p - '0') > 9) ++p;
    duck_u64 value = 0;
    do {
        value = value * 10 + (unsigned char)(*p - '0');
        ++p;
    } while ((unsigned char)(*p - '0') <= 9);
    *cursor = p;
    return value;
}

static __attribute__((always_inline)) inline duck_i64 duck_read_i64(const char **cursor) {
    const char *p = *cursor;
    while (*p != '-' && (unsigned char)(*p - '0') > 9) ++p;
    int negative = *p == '-';
    p += negative;
    duck_u64 value = 0;
    do {
        value = value * 10 + (unsigned char)(*p - '0');
        ++p;
    } while ((unsigned char)(*p - '0') <= 9);
    *cursor = p;
    return negative ? -(duck_i64)value : (duck_i64)value;
}

static __attribute__((always_inline)) inline char *duck_write_u64(char *out, duck_u64 value) {
    char tmp[24];
    unsigned n = 0;
    do {
        tmp[n++] = (char)('0' + value % 10);
        value /= 10;
    } while (value);
    do *out++ = tmp[--n]; while (n);
    return out;
}

static __attribute__((always_inline)) inline char *duck_write_i64(char *out, duck_i64 value) {
    if (value < 0) {
        *out++ = '-';
        return duck_write_u64(out, (duck_u64)(-value));
    }
    return duck_write_u64(out, (duck_u64)value);
}

static __attribute__((always_inline, noreturn)) inline void duck_exit(void) {
    __asm__ volatile("mov $60,%%eax;xor %%edi,%%edi;syscall" ::: "rax", "rdi", "rcx", "r11", "memory");
    __builtin_unreachable();
}

#endif


/*
 * Prefix-doubling suffix array.  JudgeDuck exposes stdin/stdout as memory,
 * so the implementation also bypasses libc startup and buffered stdio.
 */

typedef unsigned int u32;

enum { MAXN = 100005 };

static int sa[MAXN], rk[MAXN], nrk[MAXN], tmp[MAXN], cnt[MAXN];
static int height[MAXN];

static __attribute__((always_inline)) inline char *write_u32(char *p, u32 x) {
    char b[10];
    unsigned n = 0;
    do {
        b[n++] = (char)('0' + x % 10u);
        x /= 10u;
    } while (x);
    do *p++ = b[--n]; while (n);
    return p;
}

static void run(DuckInfo *info) {
    const unsigned char *s = (const unsigned char *)info->stdin_ptr;
    int n = 0;
    while ((unsigned)(s[n] - 'a') < 26u) ++n;

    for (int i = 0; i < 26; ++i) cnt[i] = 0;
    for (int i = 0; i < n; ++i) ++cnt[s[i] - 'a'];
    for (int i = 1; i < 26; ++i) cnt[i] += cnt[i - 1];
    for (int i = n; i--;) sa[--cnt[s[i] - 'a']] = i;

    int classes = 0;
    for (int i = 0; i < n; ++i) {
        if (i == 0 || s[sa[i]] != s[sa[i - 1]]) ++classes;
        rk[sa[i]] = classes - 1;
    }

    for (int k = 1; classes < n; k <<= 1) {
        int p = 0;
        int start = n - k;
        if (start < 0) start = 0;
        for (int i = start; i < n; ++i) tmp[p++] = i;
        for (int i = 0; i < n; ++i)
            if (sa[i] >= k) tmp[p++] = sa[i] - k;

        for (int i = 0; i < classes; ++i) cnt[i] = 0;
        for (int i = 0; i < n; ++i) ++cnt[rk[tmp[i]]];
        for (int i = 1; i < classes; ++i) cnt[i] += cnt[i - 1];
        for (int i = n; i--;) sa[--cnt[rk[tmp[i]]]] = tmp[i];

        int nc = 0;
        nrk[sa[0]] = 0;
        for (int i = 1; i < n; ++i) {
            int a = sa[i - 1], b = sa[i];
            if (rk[a] != rk[b] ||
                (a + k < n ? rk[a + k] : -1) !=
                (b + k < n ? rk[b + k] : -1))
                ++nc;
            nrk[b] = nc;
        }
        ++nc;
        for (int i = 0; i < n; ++i) rk[i] = nrk[i];
        classes = nc;
    }

    int h = 0;
    for (int i = 0; i < n; ++i) {
        int r = rk[i];
        if (r == n - 1) {
            h = 0;
            continue;
        }
        int j = sa[r + 1];
        while (i + h < n && j + h < n && s[i + h] == s[j + h]) ++h;
        height[r] = h;
        if (h) --h;
    }

    char *out = info->stdout_ptr;
    for (int i = 0; i < n; ++i) {
        out = write_u32(out, (u32)sa[i] + 1u);
        *out++ = i + 1 == n ? '\n' : ' ';
    }
    for (int i = 0; i + 1 < n; ++i) {
        out = write_u32(out, (u32)height[i]);
        *out++ = i + 2 == n ? '\n' : ' ';
    }
    if (n == 1) *out++ = '\n';
    info->stdout_size = (duck_u64)(out - info->stdout_ptr);
}

#ifndef LOCAL
__attribute__((noreturn))
void __libc_start_main(void *unused, long argc, char **argv) {
    (void)unused;
    DuckInfo *info = duck_info(argc, argv);
    run(info);
    duck_exit();
}

int main(void) {}
#else
extern long read(int, void *, unsigned long);
extern long write(int, const void *, unsigned long);
static char local_in[MAXN], local_out[2000020];
int main(void) {
    long n = read(0, local_in, sizeof(local_in));
    DuckInfo info = {0};
    info.stdin_ptr = local_in;
    info.stdin_size = (duck_u64)n;
    info.stdout_ptr = local_out;
    info.stdout_limit = sizeof(local_out);
    run(&info);
    write(1, local_out, info.stdout_size);
    return 0;
}
#endif

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #13.34 us20 KBAcceptedScore: 100

Subtask #1 Testcase #23.79 us24 KBAcceptedScore: 0

Subtask #1 Testcase #34.29 us32 KBAcceptedScore: 0

Subtask #1 Testcase #45.2 us32 KBAcceptedScore: 0

Subtask #1 Testcase #56.16 us32 KBAcceptedScore: 0

Subtask #1 Testcase #65.65 us32 KBAcceptedScore: 0

Subtask #1 Testcase #75.869 ms3 MB + 16 KBAcceptedScore: 0

Subtask #1 Testcase #823.445 ms3 MB + 236 KBAcceptedScore: 0

Subtask #1 Testcase #98.18 ms3 MB + 104 KBAcceptedScore: 0

Subtask #1 Testcase #105.142 ms2 MB + 44 KBAcceptedScore: 0

Subtask #1 Testcase #115.841 ms2 MB + 84 KBAcceptedScore: 0

Subtask #1 Testcase #1213.01 ms3 MB + 300 KBAcceptedScore: 0

Subtask #1 Testcase #1311.561 ms3 MB + 320 KBAcceptedScore: 0

Subtask #1 Testcase #149.575 ms3 MB + 132 KBAcceptedScore: 0

Subtask #1 Testcase #159.147 ms3 MB + 136 KBAcceptedScore: 0

Subtask #1 Testcase #1612.894 ms3 MB + 300 KBAcceptedScore: 0

Subtask #1 Testcase #1712.914 ms3 MB + 300 KBAcceptedScore: 0

Subtask #1 Testcase #1813.37 ms3 MB + 300 KBAcceptedScore: 0


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