#include <cstdio>
#include <algorithm>
#include <numeric>
#include <cstring>
struct IO_Tp
{
const static int _O_Buffer_Size = 10 << 20;
char _O_Buffer[_O_Buffer_Size], *_O_pos = _O_Buffer;
~IO_Tp() { fwrite(_O_Buffer, 1, _O_pos - _O_Buffer, stdout); }
IO_Tp &operator<<(int n)
{
static char _buf[10];
char* _pos = _buf;
do
*_pos++ = '0' + n % 10;
while (n /= 10);
while (_pos != _buf)
*_O_pos++ = *--_pos;
return *this;
}
IO_Tp &operator<<(char ch)
{
*_O_pos++ = ch;
return *this;
}
} IO;
const int Max_N = 1000005;
namespace SA_IS
{
int *sa, cnt[Max_N], cur[Max_N];
inline void push_S(const int s[], const int x) { sa[--cur[s[x]]] = x; }
inline void push_L(const int s[], const int x) { sa[cur[s[x]]++] = x; }
inline void induced_sort(const int n, const int m, const int n1, const int s[], const char type[], const int v[])
{
std::fill(sa, sa + n, -1);
std::fill(cnt, cnt + m, 0);
for (int i = 0; i < n; ++i)
++cnt[s[i]];
std::partial_sum(cnt, cnt + m, cnt);
std::copy(cnt, cnt + m, cur);
for (int i = n1 - 1; ~i; --i)
push_S(s, v[i]);
std::copy(cnt, cnt + m - 1, cur + 1);
for (int i = 0; i < n; ++i)
if (sa[i] > 0 && type[sa[i] - 1] == 1)
push_L(s, sa[i] - 1);
std::copy(cnt, cnt + m, cur);
for (int i = n - 1; ~i; --i)
if (sa[i] > 0 && type[sa[i] - 1] == 0)
push_S(s, sa[i] - 1);
}
void sais_core(const int n, const int m, int s[], char type[], int lms[])
{
type[n - 1] = false;
for (int i = n - 2; ~i; --i)
type[i] = s[i] == s[i + 1] ? type[i + 1] : s[i] > s[i + 1];
int n1 = 0, ch = -1;
for (int i = 1; i < n; ++i)
if (type[i - 1] == 1 && type[i] == 0)
lms[n1++] = i;
induced_sort(n, m, n1, s, type, lms);
int *s1 = s + n;
for (int i = 0, x, y; i < n; ++i)
if (sa[i] && type[sa[i] - 1] == 1 && type[sa[i]] == 0)
{
x = sa[i];
if (ch <= 0)
ch++;
else
for (int j = 0; ; ++j)
{
if (s[x + j] != s[y + j])
{
ch++;
break;
}
else if (j && ((type[x + j - 1] == 1 && type[x + j] == 0) || (type[y + j - 1] == 1 && type[y + j] == 0)))
break;
}
s1[x >> 1] = ch;
y = x;
}
for (int i = 0; i < n1; ++i)
s1[i] = s1[lms[i] >> 1];
if (ch + 1 < n1)
sais_core(n1, ch + 1, s1, type + n, lms + n1);
else
for (int i = 0; i < n1; ++i)
sa[s1[i]] = i;
for (int i = 0; i < n1; ++i)
s1[i] = lms[sa[i]];
induced_sort(n, m, n1, s, type, s1);
}
template<typename _Char>
void main(const _Char s[], const int n)
{
static int _s[Max_N << 1], _lms[Max_N];
static char _type[Max_N << 1];
int min = s[0], max = s[0];
for (int i = 1; i < n; ++i)
min = std::min<int>(min, s[i]), max = std::max<int>(max, s[i]);
for (int i = 0; i < n; ++i)
_s[i] = s[i] - min + 1;
_s[n] = 0;
sais_core(n + 1, max - min + 1 + 1, _s, _type, _lms);
}
}
void klaap(const char s[], const int sa[], int lcp[], const int n)
{
static int rk[Max_N];
for (int i = 0; i < n; ++i)
rk[sa[i]] = i;
for (int i = 0, h = lcp[0] = 0; i < n; ++i)
if (h -= h != 0, rk[i])
{
for (int j = sa[rk[i] - 1]; i + h < n && j + h < n && s[i + h] == s[j + h]; ++h)
;
lcp[rk[i]] = h;
}
}
char s[Max_N];
int N;
int sa[Max_N], lcp[Max_N];
int main(int argc, char **argv)
{
scanf("%s", s);
N = strlen(s);
SA_IS::sa = sa;
SA_IS::main(s, N);
klaap(s, sa + 1, lcp, N);
for (int i = 1; i <= N; ++i)
IO << sa[i] + 1 << " \n"[i == N];
for (int i = 1; i < N; ++i)
IO << lcp[i] << " \n"[i == N];
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Subtask #1 Testcase #1 | 14.48 us | 64 KB | Wrong Answer | Score: -100 | 显示更多 |
| Subtask #1 Testcase #2 | 17.86 us | 64 KB | Accepted | Score: 100 | 显示更多 |
| Subtask #1 Testcase #3 | 16.23 us | 64 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #4 | 16.16 us | 64 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #5 | 15.98 us | 64 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #6 | 15.63 us | 64 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #7 | 7.028 ms | 3 MB + 844 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #8 | 8.48 ms | 4 MB + 36 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #9 | 8.214 ms | 3 MB + 832 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #10 | 5.259 ms | 2 MB + 512 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #11 | 5.318 ms | 2 MB + 564 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #12 | 5.005 ms | 4 MB + 24 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #13 | 5.14 ms | 3 MB + 1020 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #14 | 5.096 ms | 3 MB + 700 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #15 | 5.184 ms | 3 MB + 752 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #16 | 5.49 ms | 4 MB + 464 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #17 | 5.499 ms | 4 MB + 556 KB | Accepted | Score: 0 | 显示更多 |
| Subtask #1 Testcase #18 | 5.405 ms | 4 MB + 552 KB | Accepted | Score: 0 | 显示更多 |