提交记录 14289


用户 题目 状态 得分 用时 内存 语言 代码长度
lyfer 1006. 【模板题】后缀排序 Wrong Answer 0 8.634 ms 5108 KB C++ 3.09 KB
提交时间 评测时间
2020-09-19 22:18:57 2020-09-19 22:19:02
#pragma GCC optimize("O3")
#pragma GCC optimize(3)

#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

const int BufferSize = 2180000;

char buffer[BufferSize];
char *out_tail = buffer;

inline void putint(int x)
{
	if (!x)
		*out_tail++ = '0';
	else
	{
		char s_pool[6], *s_tail = s_pool;
		while (x != 0)
			*s_tail++ = x % 10 + '0', x /= 10;
		while (s_tail-- != s_pool)
			*out_tail++ = *s_tail;
	}
	*out_tail++ = ' ';
}

const int N = 200005;

int n;
char s[N];

namespace SA {
int sa[N], rk[N], ht[N], s[N<<1], t[N<<1], p[N], cnt[N], cur[N];
#define pushS(x) sa[cur[s[x]]--] = x
#define pushL(x) sa[cur[s[x]]++] = x
#define inducedSort(v) \
    fill_n(sa, n, -1); fill_n(cnt, m, 0);                                     \
    for (int i = 0; i < n; i++) cnt[s[i]]++;                                  \
    for (int i = 1; i < m; i++) cnt[i] += cnt[i-1];                           \
    for (int i = 0; i < m; i++) cur[i] = cnt[i]-1;                            \
    for (int i = n1-1; ~i; i--) pushS(v[i]);                                  \
    for (int i = 1; i < m; i++) cur[i] = cnt[i-1];                            \
    for (int i = 0; i < n; i++) if (sa[i] > 0 &&  t[sa[i]-1]) pushL(sa[i]-1); \
    for (int i = 0; i < m; i++) cur[i] = cnt[i]-1;                            \
    for (int i = n-1;  ~i; i--) if (sa[i] > 0 && !t[sa[i]-1]) pushS(sa[i]-1);
void sais(int n, int m, int *s, int *t, int *p) {
    int n1 = t[n-1] = 0, ch = rk[0] = -1, *s1 = s+n;
    for (int i = n-2; ~i; i--) t[i] = s[i] == s[i+1] ? t[i+1] : s[i] > s[i+1];
    for (int i = 1; i < n; i++) rk[i] = t[i-1] && !t[i] ? (p[n1] = i, n1++) : -1;
    inducedSort(p);
    for (int i = 0, x, y; i < n; i++) if (~(x = rk[sa[i]])) {
        if (ch < 1 || p[x+1] - p[x] != p[y+1] - p[y]) ch++;
        else for (int j = p[x], k = p[y]; j <= p[x+1]; j++, k++)
            if ((s[j]<<1|t[j]) != (s[k]<<1|t[k])) {ch++; break;}
        s1[y = x] = ch;
    }
    if (ch+1 < n1) sais(n1, ch+1, s1, t+n, p+n1);
    else for (int i = 0; i < n1; i++) sa[s1[i]] = i;
    for (int i = 0; i < n1; i++) s1[i] = p[sa[i]];
    inducedSort(s1);
}
template<typename T>
int mapCharToInt(int n, const T *str) {
    int m = *max_element(str, str+n);
    fill_n(rk, m+1, 0);
    for (int i = 0; i < n; i++) rk[str[i]] = 1;
    for (int i = 0; i < m; i++) rk[i+1] += rk[i];
    for (int i = 0; i < n; i++) s[i] = rk[str[i]] - 1;
    return rk[m];
}
// Ensure that str[n] is the unique lexicographically smallest character in str.
template<typename T>
void suffixArray(int n, const T *str) {
    int m = mapCharToInt(++n, str);
    sais(n, m, s, t, p);
    for (int i = 0; i < n; i++) rk[sa[i]] = i;
    for (int i = 0, h = ht[0] = 0; i < n-1; i++) {
        int j = sa[rk[i]-1];
        while (i+h < n && j+h < n && s[i+h] == s[j+h]) h++;
        if (ht[rk[i]] = h) h--;
    }
}
};

int main()
{
	scanf("%s", s);
	n = strlen(s);
	s[n] = 'a' - 1;

	SA::suffixArray(n, s);

	for (int i = 1; i <= n; ++i)
		putint(SA::sa[i] + 1);
	*out_tail++ = '\n';
	for (int i = 2; i <= n; ++i)
		putint(SA::ht[i]);

	fwrite(buffer, 1, out_tail - buffer, stdout);
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #110.76 us56 KBWrong AnswerScore: -100

Subtask #1 Testcase #214.1 us56 KBAcceptedScore: 100

Subtask #1 Testcase #312.93 us56 KBAcceptedScore: 0

Subtask #1 Testcase #412.31 us56 KBAcceptedScore: 0

Subtask #1 Testcase #512.07 us56 KBAcceptedScore: 0

Subtask #1 Testcase #612.29 us56 KBAcceptedScore: 0

Subtask #1 Testcase #77.442 ms4 MB + 196 KBAcceptedScore: 0

Subtask #1 Testcase #88.634 ms4 MB + 380 KBAcceptedScore: 0

Subtask #1 Testcase #98.421 ms4 MB + 164 KBAcceptedScore: 0

Subtask #1 Testcase #105.389 ms2 MB + 740 KBAcceptedScore: 0

Subtask #1 Testcase #115.339 ms2 MB + 780 KBAcceptedScore: 0

Subtask #1 Testcase #125.282 ms4 MB + 304 KBAcceptedScore: 0

Subtask #1 Testcase #135.21 ms4 MB + 240 KBAcceptedScore: 0

Subtask #1 Testcase #145.37 ms3 MB + 872 KBAcceptedScore: 0

Subtask #1 Testcase #155.356 ms3 MB + 928 KBAcceptedScore: 0

Subtask #1 Testcase #165.642 ms4 MB + 892 KBAcceptedScore: 0

Subtask #1 Testcase #175.636 ms4 MB + 1012 KBAcceptedScore: 0

Subtask #1 Testcase #185.631 ms4 MB + 976 KBAcceptedScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2026-03-22 19:52:53 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠