Lexicographical Substring Search(SAM——求字典序第k小的子串)

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.

Example

Input:
aaa
2
2
3

Output:
aa
aaa

Edited: Some input file contains garbage at the end. Do not process them.

对串创建SAM,然后,拓扑一下,算出每个状态,如果继续往后边走,能够生成多少种不同的字符串。然后每次查询的时候,就按照字典顺序贪心着找

//#include<cstdio>
//#include<cstring>
//#include<string>
//#include<iostream>
//#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;//字符串长度 
const int maxc=26;
char s[maxn];
struct Suffix_Automaton {//打*的不一定用到 
    int next[maxn<<1][maxc];  //状态转移(尾部加一个字符的下一个状态)
	int len[maxn<<1]; //最长子串的长度(该节点子串数量=len[x]-len[link[x]])
	int link[maxn<<1];   //后缀链接(最短串前部减少一个字符所到达的状态)
//	int cnt[maxn<<1];   //被后缀连接的数(*) 
	int id; //结点编号
	int last; //最后结点
	int endpos[maxn<<1]; // endpos数(一类子串的数量)
	int a[maxn];		
    int b[maxn<<1];
    int sum[maxn<<1];//该节点后面所形成的子串的总数
//    int dp[maxn<<1];
//    int ans[maxn<<1];
//    ll d[maxn<<1];   //d[i]表示从状态i出发,不同的子串的数目,即不同的路径数
	void init() {	//初始化
		for(int i=1; i<=id; i++){ //常规初始化 
			link[i] = len[i] = 0;
			memset(next[i],0,sizeof(next[i]));
			endpos[i]=0;
			a[i]=0;
			b[i]=0;
		}
//		for(int i=1;i<=id;i++) {//非常规初始化 
//			d[i]=0;
//		}
		last = id = 1; //1表示root起始点 空集
	}
//SAM建图
	void add(int c) {     //插入字符,为字符ascll码值
		int x = ++id; //创建一个新结点x;
		len[x] = len[last] + 1; //  长度等于最后一个结点+1
		endpos[x] = 1;  //接受结点子串除后缀连接还需加一
		int p;  //第一个有C转移的结点;
		for (p = last; p && !next[p][c]; p = link[p])
			next[p][c] = x;//沿着后缀连接 将所有没有字符c转移的节点直接指向新结点
		if (!p){   //全部都没有c的转移 直接将新结点后缀连接到起点
			link[x] = 1;
		//	cnt[1]++;  
	    }
		else {
			int q = next[p][c];    //p通过c转移到的结点
			if (len[p] + 1 == len[q]){//pq是连续的
				link[x] = q;
			//	cnt[q]++; //将新结点后缀连接指向q即可,q结点的被后缀连接数+1
			} 
			else {
				int nq = ++id;   //不连续 需要复制一份q结点
				len[nq] = len[p] + 1;   //令nq与p连续
				link[nq] = link[q];   //因后面link[q]改变此处不加cnt
				memcpy(next[nq], next[q], sizeof(next[q]));  //复制q的信息给nq
				for (; p&&next[p][c] == q; p = link[p])
					next[p][c] = nq;    //沿着后缀连接 将所有通过c转移为q的改为nq
				link[q] = link[x] = nq; //将x和q后缀连接改为nq
				//cnt[nq] += 2; //  nq增加两个后缀连接	
			}
		}
		last = x;  //更新最后处理的结点	
	} 
	/*
	ll getSubNum() {	//求不相同子串数量
		ll ans = 0;
		for (int i = 2; i <= id; i++)
			ans += len[i]-len[link[i]];	//一状态子串数量等于len[i]-len[link[i]]
		return ans;
	} 
	*/
	void getTP(int &Len){//对sam的节点按照len,从小到大排序重新标号,即给定节点的拓扑序
	    for(int i=1;i<=id;i++) a[len[i]]++;
		for(int i=1;i<=Len;i++) a[i]+=a[i-1];
		for(int i=1;i<=id;i++) b[a[len[i]]--]=i;
	}
	void getendpos(){//求每类子串的数量 ,即endpos集合的大小 
		for(int i=id;i>=1;i--){ //按拓扑序遍历 
			int e=b[i];
			endpos[link[e]]+=endpos[e];
		} 
	}
	/*
	void get_len_max(int Len){//求长度为i的出现次数最多的子串的出现次数 
		for(int i=1;i<=id;i++) dp[len[i]]=max(dp[len[i]],endpos[i]);
		for(int i=Len-1;i>=1;i--) dp[i]=max(dp[i],dp[i+1]);
		for(int i=1;i<=Len;i++) printf("%d\n",dp[i]);
	}
	*/
	void getsum(){
		for(int i=id;i>=1;i--){
			int &e=b[i];
			sum[e]=1;
			for(int j=0;j<26;j++){
				sum[e]+=sum[next[e][j]];
			}
		}
	}
	void getmink(int k){
		int now=1,p;
		string s="";
		while(k){
			for(int i=0;i<26;i++){
				if(next[now][i]&&k){
					p=next[now][i];
					if(sum[p]<k) k-=sum[p];
					else{
						k--;
						now=p;
						s+=(char)(i+'a');
						break;
					}
				}
			}
		}
		cout<<s<<endl;
	}
	/*
	void LCS1(char s[],int Len){//求两个串的最长公共子串
	    int ans=0,cnt=0;
	    int now=1;
	    char base='a';
	    for(int i=0;i<Len;i++){
	      	int c=s[i]-base;
	      	if(next[now][c]){
	      	    cnt++;
	      	    now=next[now][c];
			}
			else{
			  	while(now&&!next[now][c]) now=link[now];
			  	if(!now) cnt=0,now=1;
			  	else cnt=len[now]+1,now=next[now][c];
			}
			ans=max(ans,cnt);
		}
		printf("%d\n",ans);
	} 
	*/
	
	/*
	void init_ans(){
		for(int i=1;i<=id;i++) ans[i]=len[i];
	}
	void LCS2(char s[],int Len){     //求多个串的最长公共子串 
		for(int i=1;i<=id;i++) dp[i]=0;
		int cnt=0;
	    int now=1;
	    char base='a';
	    for(int i=0;i<Len;i++){
	      	int c=s[i]-base;
	      	if(next[now][c]){
	      	    cnt++;
	      	    now=next[now][c];
			}
			else{
			  	while(now&&!next[now][c]) now=link[now];
			  	if(!now) cnt=0,now=1;
			  	else cnt=len[now]+1,now=next[now][c];
			}
			dp[now]=max(dp[now],cnt);
		}
		for(int i=id;i>=1;i--){
			int e=b[i];
			dp[link[e]]=max(dp[link[e]],min(dp[e],len[link[e]]));
		}
		for(int i=1;i<=id;i++) ans[i]=min(ans[i],dp[i]);
	}
	void get_LCS2_ans(){
		int cnt=0;
		for(int i=1;i<=id;i++) cnt=max(cnt,ans[i]);
		printf("%d\n",cnt);
	}
	*/
	
	/*
	void solve1(){ //求出现次数为k的子串种数 
		ll ans=0;
		for(int i=1;i<=id;i++){
			if(endpos[i]==K){
				ans+=len[i]-len[link[i]];
			}
		}
		printf("%lld\n",ans);
	}
	 */
	 
	/*
	void solve1(){//求出现次数A<=K<=B的子串种数 
		for(int i=id;i>1;i--){
			int v=b[i];
			if(endpos[v]>=A&&endpos[v]<=B) d[v]++;
			for(int j=0;j<26;j++){
				if(next[v][j]) d[v]+=d[next[v][j]];
			}
		}
		ll ans=0;
		for(int i=0;i<26;i++){
			if(next[1][i]) ans+=d[next[1][i]];
		}
		printf("%lld\n",ans);
	}
	*/
	
	/*
	void solve2(){//求出现次数>=k的子串的最大长度 
		int ans=0;
		for(int i=1;i<=id;i++){
			if(endpos[i]>=K){
				ans=max(ans,len[i]);
			}
		}
		printf("%d\n",ans);
	}*/
	
} sam;
int main(){
    scanf("%s",s);
    int len=strlen(s);
    sam.init();
    for(int i=0;i<len;i++) sam.add(s[i]-'a');
    sam.getTP(len);
	sam.getsum(); 
    int T;
    scanf("%d",&T);
    while(T--){
    	int K;
    	scanf("%d",&K);
    	sam.getmink(K);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生参加学科竞赛有着诸多好处,不仅有助于个人综合素质的提升,还能为未来职业发展奠定良好基础。以下是一些分析: 首先,学科竞赛是提高专业知识和技能水平的有效途径。通过参与竞赛,学生不仅能够深入学习相关专业知识,还能够接触到最新的科研成果和技术发展趋势。这有助于拓展学生的学科视野,使其对专业领域有更深刻的理解。在竞赛过程中,学生通常需要解决实际问题,这锻炼了他们独立思考和解决问题的能力。 其次,学科竞赛培养了学生的团队合作精神。许多竞赛项目需要团队协作来完成,这促使学生学会有效地与他人合作、协调分工。在团队合作中,学生们能够学到如何有效沟通、共同制定目标和分工合作,这对于日后进入职场具有重要意义。 此外,学科竞赛是提高学生综合能力的一种途径。竞赛项目通常会涉及到理论知识、实际操作和创新思维等多个方面,要参赛者具备全面的素质。在竞赛过程中,学生不仅需要展现自己的专业知识,还需要具备创新意识和解决问题的能力。这种全面的综合能力培养对于未来从事各类职业都具有积极作用。 此外,学科竞赛可以为学生提供展示自我、树立信心的机会。通过比赛的舞台,学生有机会展现自己在专业领域的优势,得到他人的认可和赞誉。这对于培养学生的自信心和自我价值感非常重要,有助于他们更加积极主动地投入学习和未来的职业生涯。 最后,学科竞赛对于个人职业发展具有积极的助推作用。在竞赛中脱颖而出的学生通常能够引起企业、研究机构等用人单位的关注。获得竞赛奖项不仅可以作为个人履历的亮点,还可以为进入理想的工作岗位提供有力的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值