# 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;
}```

1811 篇文章121 人订阅

0 条评论

## 相关文章

### HUDOJ-----1394Minimum Inversion Number

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit:...

37460

### Socket与Http方式解析发送xml消息封装中间件jar包

最近项目代码中太多重复的编写Document,不同的接口需要不同的模板，于是重写提取公共部分打成jar包，方便各个系统统一使用~

17130

17510

### HDUOJ 2672---god is a girl 《斐波那契数》

god is a girl Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3276...

28960

47860

10630

8420

### BZOJ 1257: [CQOI2007]余数之和sum【神奇的做法，思维题】

1257: [CQOI2007]余数之和sum Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 4474  So...

26540

### 2301: [HAOI2011]Problem b

2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MB Submit: 1737...

26450

### 1677: [Usaco2005 Jan]Sumsets 求和

1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 6...

30880