SPOJ - SUBLEX Lexicographical Substring Search (后缀自动机)

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.

题意为求字典序排第k名的子串

分析:我们可以从根节点开始一个一个来进行构造,比如ab,  如果先确定一个a,那么后面就有2个子串,a和ab,也就是说只要k<=2那么我们就先确定一个a,否则的话就继续找一个字母b

像这样就可以层层递进,最终可以确定题目中要求的字符串。

另外,我们需要先处理出当节点p确定后,产生的字符串的数目

代码如下:

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>

using namespace std;
const int N=100010;
typedef long long ll;
struct State
{
    State *link,*go[26];
    int step;
    int num;
    void clear()
    {
        num=0;
        link=0;
        step=0;
        memset(go,0,sizeof(go));
    }
}*root,*last;
int cnt[N];
int lenA;
State statePool[N*2],*b[N*2],*cur;

void init()
{
    cur=statePool;
    root=last=cur++;
    root->clear();
}

void Insert(int w)
{
    State *p=last;
    State *np=cur++;
    np->clear();
    np->step=p->step+1;
    while(p&&!p->go[w])
        p->go[w]=np,p=p->link;
    if(p==0)
        np->link=root;
    else
    {
        State *q=p->go[w];
        if(p->step+1==q->step)
            np->link=q;
        else
        {
            State *nq=cur++;
            nq->clear();
            memcpy(nq->go,q->go,sizeof(q->go));
            nq->step=p->step+1;
            nq->link=q->link;
            q->link=nq;
            np->link=nq;
            while(p&&p->go[w]==q)
                p->go[w]=nq, p=p->link;
        }
    }
    last=np;
}

void tsort()
{
    memset(cnt,0,sizeof(cnt));
    State *p;
    for(p=statePool;p!=cur;p++)
      cnt[p->step]++;
    for(int i=1;i<=lenA;i++)
      cnt[i]+=cnt[i-1];
    for(p=statePool;p!=cur;p++)
      b[--cnt[p->step]]=p;
}
char A[N],B[N];
int ans[N],ct[N*2];
char ch[N*2][30];
void solve(int k)
{
    char ans[N];
    int cnt1=0;
    State *p=root;
    while(k>0)
    {
       int id=p-statePool;
       for(int i=0;i<ct[id];i++)
       {
          if(k>p->go[ch[id][i]-'a']->num)
          k-=p->go[ch[id][i]-'a']->num;
          else
          {
            ans[cnt1++]=ch[id][i];
            k--;
            if(k<=0)
            break;
            p=p->go[ch[id][i]-'a'];
            break;
          }
       }
    }
    ans[cnt1]=0;
    printf("%s\n",ans);
}
int main()
{
    scanf("%s",A);
    lenA=strlen(A);
    init();
    for(int i=0;i<lenA;i++)
        Insert(A[i]-'a');
        tsort();
    memset(ct,0,sizeof(ct));
    int L=cur-statePool;
    State *p;
    for(p=statePool;p!=cur;p++)
    p->num=1;
    for(int i=L-1;i>=0;i--)
    {
        p=b[i];
        int id=p-statePool;
        for(int j=0;j<26;j++)
        {
          if(p->go[j])
          {
            p->num+=p->go[j]->num;  //求出当节点p确定时,产生的字符串的可能数
            ch[id][ct[id]++]='a'+j;//记录该节点后面,可能连接的字母
          }
        }
    }
    int q,k;
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d",&k);
        solve(k);
    }
    return 0;
}

 

posted @ 2017-10-21 15:14  hinata_hajime  阅读(182)  评论(0编辑  收藏  举报