# BZOJ 3998: [TJOI2015]弦论

Time Limit: 10 Sec
Memory Limit: 256 MB

Description

Input

Output

Sample Input

aabc

0 3
Sample Output

aab

HINT

N<=5*10^5

T<2

K<=10^9

#### Code####

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<vector>
using namespace std;
#define PA pair<int,int>
{
int s=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0';ch=getchar();}
return s*f;
}

const int N=500005;
struct samm
{
int last,total,n;
int L[N*2],ch[N*2][26],fa[N*2];
int val[N*2],sum[N*2],t[N],h[N*2];
samm(){total=1,last=1;}
void insert(int C)
{
int p=last,now=last=++total;
L[now]=L[p]+1;n=L[now];val[now]=1;
for(;p&&!ch[p][C];p=fa[p])
ch[p][C]=now;
if(!p)fa[now]=1;
else
if(L[ch[p][C]]==L[p]+1)
fa[now]=ch[p][C];
else
{int ne=++total,Q=ch[p][C];
memcpy(ch[ne],ch[Q],sizeof(ch[Q]));
fa[ne]=fa[Q];
L[ne]=L[p]+1;
fa[Q]=fa[now]=ne;
for(;p&&ch[p][C]==Q;p=fa[p])
ch[p][C]=ne;
}
}
void pre(int T)
{for(int i=1;i<=total;i++)t[L[i]]++;
for(int i=1;i<=n;i++)t[i]+=t[i-1];
for(int i=total;i;i--)h[t[L[i]]--]=i;
for(int i=total,x;i;i--)
{x=h[i];
if(T)val[fa[x]]+=val[x];
else val[x]=1;
}
val[1]=0;
for(int i=total,x;i;i--)
{x=h[i];sum[x]=val[x];
for(int j=0;j<26;j++)
sum[x]+=sum[ch[x][j]];
}
}
void dfs(int x,int K)
{K-=val[x];
if(K<=0)return;
for(int i=0,v;i<26;i++)
if(v=ch[x][i])
{if(K-sum[v]<=0)
{putchar(i+'a');
dfs(v,K);
break;
}
K-=sum[v];
}
}
}a;
char z[N];
int n,k,t;
int main()
{
scanf("%s",z);
n=strlen(z);
for(int i=0;i<n;i++)
a.insert(z[i]-'a');
a.pre(t);
if(a.sum[1]<k)
puts("-1");
else
a.dfs(1,k);
return 0;
}



posted on 2016-03-12 20:27  wuyuhan  阅读(183)  评论(0编辑  收藏  举报