#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
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Subtask #1 Testcase #1 | 3.34 us | 20 KB | Accepted | Score: 100 | 显示更多 |
| Subtask #1 Testcase #2 | 3.79 us | 24 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #3 | 4.29 us | 32 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #4 | 5.2 us | 32 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #5 | 6.16 us | 32 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #6 | 5.65 us | 32 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #7 | 5.869 ms | 3 MB + 16 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #8 | 23.445 ms | 3 MB + 236 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #9 | 8.18 ms | 3 MB + 104 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #10 | 5.142 ms | 2 MB + 44 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #11 | 5.841 ms | 2 MB + 84 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #12 | 13.01 ms | 3 MB + 300 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #13 | 11.561 ms | 3 MB + 320 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #14 | 9.575 ms | 3 MB + 132 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #15 | 9.147 ms | 3 MB + 136 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #16 | 12.894 ms | 3 MB + 300 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #17 | 12.914 ms | 3 MB + 300 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #18 | 13.37 ms | 3 MB + 300 KB | Accepted | Score: 0 | 显示更多 |