# BZOJ3998: [TJOI2015]弦论(后缀自动机)

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 4018  Solved: 1477
[Submit][Status][Discuss]

## Input

第一行是一个仅由小写英文字母构成的字符串S

aabc
0 3

aab

N<=5*10^5

T<2

K<=10^9

## Source

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = 1e6 + 10;
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
char s[MAXN];
int opt, K, N;
int fa[MAXN], len[MAXN], ch[MAXN][27], siz[MAXN], right[MAXN], tot = 1, last = 1, root = 1;
void insert(int x) {
int now = ++tot, pre = last; last = now; len[now] = len[pre] + 1;
right[now] = 1;
for(; pre && !ch[pre][x]; pre = fa[pre]) ch[pre][x] = now;
if(!pre) fa[now] = root;
else {
int q = ch[pre][x];
if(len[q] == len[pre] + 1) fa[now] = q;
else {
int nows = ++tot; len[nows] = len[pre] + 1;
memcpy(ch[nows], ch[q], sizeof(ch[q]));
fa[nows] = fa[q]; fa[q] = fa[now] = nows;
for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nows;
}
}
}
void Query(int K) {
int now = root;
if(K > siz[root]) return (void) printf("-1");
while(K) {
if(now != root) K -= right[now];
if(K <= 0) break;
for(int i = 0; i <= 25; i++)
if(ch[now][i]) {
if(siz[ch[now][i]] >= K) {putchar(i + 'a'); now = ch[now][i]; break; }
else K -= siz[ch[now][i]];
}
}
puts("");
}
void Topsort() {
static int A[MAXN], a[MAXN];
for(int i = 1; i <= tot; i++) A[len[i]]++;
for(int i = 1; i <= N; i++)   A[i] += A[i - 1];
for(int i = tot; i >= 1; i--) a[A[len[i]]--] = i;
//for(int i = 1; i <= tot; i++) siz[i] = 1;

if(opt == 1)
for(int i = tot; i; i--) right[fa[a[i]]] += right[a[i]];
if(opt == 0)
for(int i = tot; i; i--) right[a[i]] = 1;
for(int i = tot; i; i--) {
siz[a[i]] = right[a[i]];
for(int j = 0; j <= 25; j++)
siz[a[i]] += siz[ch[a[i]][j]];
}
}
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#endif
scanf("%s", s + 1);
N = strlen(s + 1);
for(int i = 1; i <= N; i++) insert(s[i] - 'a');
Topsort();
Query(K);
return 0;
}

posted @ 2018-06-28 15:52 自为风月马前卒 阅读(...) 评论(...) 编辑 收藏

……