http://www.elijahqi.win/archives/3001
题意翻译
给定一个字符串,求排名第k小的串
注意样例的\n是换行
输入格式:
第一行给定主串(len<=90000)
第二行给定询问个数T<=500
随后给出T行T个询问,每次询问排名第k小的串,范围在int内
输出格式:
对于每一个询问,输出T行,每行为排名第k小的串
感谢@Creeper_LKF 提供的翻译
题目描述
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! Knowing that, his friend Kinan decided to test his skills so he gave him a string S and asked him Q questions of the form:
If all distinct substrings of string S were sorted lexicographically, which one will be the K-th smallest?
After knowing the huge number of questions Kinan will ask, Daniel figured out that he can’t do this alone. Daniel, of course, knows your exceptional programming skills, so he asked you to write him a program which given S will answer Kinan’s questions.
Example:
S = “aaa” (without quotes)
substrings of S are “a” , “a” , “a” , “aa” , “aa” , “aaa”. The sorted list of substrings will be:
“a”, “aa”, “aaa”.
Input
In the first line there is Kinan’s string S (with length no more than 90000 characters). It contains only small letters of English alphabet. The second line contains a single integer Q (Q <= 500) , the number of questions Daniel will be asked. In the next Q lines a single integer K is given (0 < K < 2^31).
Output
Output consists of Q lines, the i-th contains a string which is the answer to the i-th asked question.
输入输出格式
输入格式:
输出格式:
输入输出样例
输入样例#1: 复制
\naaa\n2\n2\n3\n\n
输出样例#1: 复制
aa\naaa\n
类似tjoi2015弦论 处理之后类似二分 针对每次询问 暴力去sam中处理即可
#include<cstring>
#include<algorithm>
#define N 180010
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
return x*f;
}
char s[N>>1];int c[N>>1],sum[N],size[N],fa[N],len[N],root=1,cnt=1,last=1,rk[N],ch[N][26];
inline void read_s(){
int op=0;char ch=gc();
while(ch<'a'||ch>'z') ch=gc();while(ch<='z'&&ch>='a') s[op++]=ch,ch=gc();
}
inline void insert1(int x){
int np=++cnt,p=last;fa[np]=p;len[np]=len[p]+1;
for (;p&&!ch[p][x];p=fa[p]) ch[p][x]=np;
if (!p) fa[np]=root;else{
int q=ch[p][x];if (len[p]+1==len[q]) fa[np]=q;else{
int nq=++cnt;memcpy(ch[nq],ch[q],sizeof(ch[q]));fa[nq]=fa[q];fa[q]=fa[np]=nq;
len[nq]=len[p]+1;for (;p&&ch[p][x]==q;p=fa[p]) ch[p][x]=nq;
}
}last=np;
}
inline void dfs(int x,int k){
if(k<=size[x]) return;k-=size[x];
for (int i=0;i<26;++i)
if(ch[x][i]){
if (k<=sum[ch[x][i]]){putchar('a'+i);dfs(ch[x][i],k);return;}
k-=sum[ch[x][i]];
}
}
int main(){
// freopen("spoj7258.in","r",stdin);
read_s();int le=strlen(s);
for (int i=0;i<le;++i) insert1(s[i]-'a');
for (int i=1;i<=cnt;++i) ++c[len[i]];
for (int i=1;i<=le;++i) c[i]+=c[i-1];
for (int i=1;i<=cnt;++i) rk[c[len[i]]--]=i;
for (int i=cnt;i;--i){int p=rk[i];size[p]=1;}size[1]=0;
for (int i=cnt;i;--i){
int p=rk[i];sum[p]=size[p];
for (int j=0;j<26;++j) sum[p]+=sum[ch[p][j]];
}int T=read(),k;
while(T--) k=read(),dfs(root,k),puts("");
return 0;
}