后缀自动机(输出字典序为K的子串)spoj7258

SPOJ Problem Set (classical)

7258. Lexicographical Substring Search

Problem code: SUBLEX

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


spoj出问题了。。。交不上,先贴一下代码

先对其排序,然偶每个节点记录一个cnt值,表示有多少个子串到达这个节点,然后从root往下搜索就行了

spoj时间果然卡的紧,TLE了好多次

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn=100010;
const int SIGMA_SIZE=26;
struct SAM_Node
{
    SAM_Node *par,*next[SIGMA_SIZE];
    int len,id;
    int cnt;
    SAM_Node() {}
    SAM_Node(int _len)
    {
        par=0;
        len=_len;
        cnt=0;
        memset(next,0,sizeof(next));
    }
};
SAM_Node node[maxn*2],*root,*last;
int SAM_size;
SAM_Node *newSAM_Node(int len)
{
    node[SAM_size]=SAM_Node(len);
    node[SAM_size].id=SAM_size;
    return &node[SAM_size++];
}
SAM_Node *newSAM_Node(SAM_Node *p)
{
    node[SAM_size]=*p;
    node[SAM_size].id=SAM_size;
    return &node[SAM_size++];
}
void SAM_add(int x,int len)
{
    SAM_Node *p=last,*np=newSAM_Node(p->len+1);
    last=np;
    while(p&&!p->next[x])
    {
        p->next[x]=np;
        p=p->par;
    }
    if(!p)np->par=root;
    else
    {
        SAM_Node *q=p->next[x];
        if(q->len==p->len+1)
            np->par=q;
        else
        {
            SAM_Node *nq=newSAM_Node(q);
            nq->len=p->len+1;
            q->par=nq;
            np->par=nq;
            while(p&&p->next[x]==q)
            {
                p->next[x]=nq;
                p=p->par;
            }
        }
    }
}
void SAM_init()
{
    SAM_size=0;
    root=last=newSAM_Node(0);
}
void SAM_build(char *s)
{
    SAM_init();
    int len=strlen(s);
    for(int i=0;inode[son[now][i]].cnt)
                x-=node[son[now][i]].cnt;
            else
            {
                s[l++]=ch[now][i];
                now=son[now][i];
                x--;
                break;
            }
        }
    }
    s[l]='\0';
    printf("%s\n",s);
}
int main()
{
    scanf("%s",s);
    SAM_build(s);
    int len=strlen(s);
    for(int i=0; i=0; i--)sa[--cnt[node[i].len]]=&node[i];
    for(int i=0; i=0; i--)
    {
        SAM_Node *tmp=sa[i];
        for(int j=0; j<26; j++)
        {
            if(!tmp->next[j])continue;
            int u=tmp-node,v=tmp->next[j]-node;
            son[u][c[u]]=v;
            ch[u][c[u]++]=j+'a';
            tmp->cnt+=tmp->next[j]->cnt;
        }
    }
    int Q;
    scanf("%d",&Q);
    while(Q--)
    {
        int x;
        scanf("%d",&x);
        solve(x);
    }
    return 0;
}




你可能感兴趣的:(后缀自动机SAM)