Description
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.
Sample Input
aaa
2
2
3
Sample Output
aa
aaa
Edited
Some input file contains garbage at the end. Do not process them.
我们可以联想到字符串的所有子串都会对应到SAM的一个状态
首先对原串建SAM,然后考虑对每个点,计算从他出发还能走到多少个子串
这里我刚开始写sb了,要注意一个点的贡献不是maxi-mini,因为我已经固定了前面的路径,这部分会算多,联想把S的任意子串扔进SAM,一定会沿着转移边走到一个节点,所以每个点的贡献就是1,直接数size就好了
然后从SAM的根节点出发,每次按照边的字典序访问,如果当前sz小于k就换下一条边,否则沿着这条边走下去,把这条边对应的字符加到答案里
写的有点混乱,贴代码
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define LB long double
#define ull unsigned long long
#define x first
#define y second
#define pb push_back
#define pf push_front
#define mp make_pair
#define Pair pair<int,int>
#define pLL pair<LL,LL>
#define pii pair<double,double>
const int INF=2e9;
const LL LINf=2e16;
const int magic=348;
const int MOD=998244353;
const double eps=1e-10;
const double pi=acos(-1);
inline int getint()
{
bool f;char ch;int res;
while (!isdigit(ch=getchar()) && ch!='-') {}
if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
while (isdigit(ch=getchar())) res=res*10+ch-'0';
return f?res:-res;
}
const int MAXN=1e5;
char s[MAXN+48];int len;
namespace SAM
{
int nxt[MAXN*2][30],par[MAXN*2],maxn[MAXN*2],minn[MAXN*2],tot;
int root,last;
inline void init() {root=last=++tot;}
inline void insert(char ch)
{
int p=last,np=++tot,w=ch-'a'+1;maxn[np]=maxn[p]+1;
while (p && !nxt[p][w]) nxt[p][w]=np,p=par[p];
if (!p)
par[np]=root;
else
{
int q=nxt[p][w];
if (maxn[q]==maxn[p]+1)
par[np]=q;
else
{
int nq=++tot;maxn[nq]=maxn[p]+1;
memcpy(nxt[nq]+1,nxt[q]+1,26*sizeof(int));
par[nq]=par[q];par[q]=par[np]=nq;
while (p && nxt[p][w]==q) nxt[p][w]=nq,p=par[p];
}
}
last=np;
}
}
using namespace SAM;
int sz[MAXN*2],sm[MAXN*2];
inline int dfs(int cur)
{
if (sz[cur]!=-1) return sz[cur];
if (cur!=1) sz[cur]=1;
for (register int i=1;i<=26;i++)
if (nxt[cur][i]) sz[cur]+=dfs(nxt[cur][i]);
return sz[cur];
}
string ans;
inline void getans(int cur,int k)
{
int i;if (cur!=1) k--;
if (!k) return;
for (i=1;i<=26;i++)
if (nxt[cur][i])
{
if (sz[nxt[cur][i]]>=k) {ans+=string(1,'a'+i-1);getans(nxt[cur][i],k);return;}
k-=sz[nxt[cur][i]];
}
}
int main ()
{
int i;scanf("%s",s+1);len=strlen(s+1);
init();for (i=1;i<=len;i++) insert(s[i]);
for (i=2;i<=tot;i++) minn[i]=maxn[par[i]]+1;
memset(sz,-1,sizeof(sz));
dfs(1);int q=getint(),k;
while (q--)
{
k=getint();ans="";
getans(1,k);cout<<ans<<endl;
}
return 0;
}