zoukankan      html  css  js  c++  java
  • Spoj SUBLEX

    Dicription

    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.

    建个后缀自动机之后,记录一下每个节点能到的节点数,然后询问的时候一遍dfs就行了。

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #define ll long long
    #define maxn 400005
    using namespace std;
    int f[maxn],ch[maxn][26];
    int l[maxn],siz[maxn],n;
    int m,q,pre=1,cnt=1,rt[maxn];
    int a[maxn],c[maxn],tot[maxn];
    char s[maxn];
    
    inline void ins(int x,int y){
    	int p=pre,np=++cnt;
    	pre=np,l[np]=l[p]+1;
    	rt[np]=y+1;
    	
    	for(;p&&!ch[p][x];p=f[p]) ch[p][x]=np;
    	if(!p) f[np]=1;
    	else{
    		int q=ch[p][x];
    		if(l[q]==l[p]+1) f[np]=q;
    		else{
    			int nq=++cnt;
    			l[nq]=l[p]+1;
    			memcpy(ch[nq],ch[q],sizeof(ch[q]));
    			f[nq]=f[q];
    			f[q]=f[np]=nq;
    			for(;ch[p][x]==q;p=f[p]) ch[p][x]=nq;
    		}
    	}
    }
    
    void dfs(int x){
    	tot[x]=siz[x]=1;
    	for(int i=0;i<26;i++) if(ch[x][i]){
    		if(!siz[ch[x][i]]) dfs(ch[x][i]);
    		tot[x]+=tot[ch[x][i]];
    	}
    }
    
    inline void build(){
    	n=strlen(s);
    	for(int i=0;i<n;i++) ins(s[i]-'a',i);
    	for(int i=1;i<=cnt;i++) c[l[i]]++;
    	for(int i=n;i>=0;i--) c[i]+=c[i+1];
    	for(int i=1;i<=cnt;i++) a[c[l[i]]--]=i;
    	for(int i=1;i<=cnt;i++){
    		int now=a[i];
    		rt[f[now]]=max(rt[f[now]],rt[now]);
    	}
    	
    	dfs(1);
    //	for(int i=1;i<=cnt;i++) cout<<tot[i]<<' '<<siz[i]<<endl;
    }
    
    void dfs2(int x){
    	if(m<=siz[x]){
    		m=0,puts("");
    		return;
    	}
    	
    	m-=siz[x];
    	
    	for(int i=0;i<26;i++){
    		int to=ch[x][i];
    		if(m<=tot[to]) putchar('a'+i),dfs2(to);
    		else m-=tot[to];
    		
    		if(!m) return;
    	}
    }
    
    inline void solve(){
    	dfs2(1);
    }
    
    int main(){
    	scanf("%s",s);
    	build();
    	
    	scanf("%d",&q);
    	while(q--){
    		scanf("%d",&m),m++;
    		solve();
    	}
    	
    	return 0;
    }
    

      

  • 相关阅读:
    千年不曾看懂《道德经》,直至有了《道德图》!--作者:南山空同
    初探工作流的库表设计
    教你如何快速上手markdown语法,编写技术博客(史上最全最简,用MarkDown写博客)
    面试官:你连RESTful都不知道我怎么敢要你? 文章解析
    Dapper系列 作者:懒懒的程序员一枚
    为何要编写《元灵心经》养、和、消三篇 作者 南山空同
    南山空同《学经》前24章
    ASP.NET Core 2.2 WebApi 系列【九】使用SignalR (作者:tenghao510 ) 学习及内容补充
    Net Core 中WebAPI有关 Session的设置,及获取
    asp.net core系列 WebAPI 作者:懒懒的程序员一枚
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8461531.html
Copyright © 2011-2022 走看看