SPOJ7258(后缀自动机--第k大的子串)

题目http://www.spoj.com/problems/SUBLEX/

 

题意:给一个字符串,然后给Q个询问,每个询问输入一个数K,输出子串中字典序为K的字符串。

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

using namespace std;
const int N=250005;

struct State
{
    State *pre,*go[26];
    int step,v;
    void clear()
    {
        v=0;
        pre=0;
        step=0;
        memset(go,0,sizeof(go));
    }
}*root,*last;

State statePool[N*2],*b[2*N],*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->pre;
    if(p==0)
        np->pre=root;
    else
    {
        State *q=p->go[w];
        if(p->step+1==q->step)
            np->pre=q;
        else
        {
            State *nq=cur++;
            nq->clear();
            memcpy(nq->go,q->go,sizeof(q->go));
            nq->step=p->step+1;
            nq->pre=q->pre;
            q->pre=nq;
            np->pre=nq;
            while(p&&p->go[w]==q)
                p->go[w]=nq, p=p->pre;
        }
    }
    last=np;
}

char str[N];
int son[2*N][26];
char ch[2*N][26];
int cnt[2*N],c[2*N];

void Solve(int k)
{
    int ct=0;
    int now=0;
    while(k)
    {
        for(int i=0; i<c[now]; i++)
        {
            State *tmp=statePool+son[now][i];
            if(k>tmp->v)
                k-=tmp->v;
            else
            {
                str[ct++]=ch[now][i];
                now=son[now][i];
                k--;
                break;
            }
        }
    }
    str[ct]=0;
    puts(str);
}

int main()
{
    int n;
    scanf("%s",str);
    n=strlen(str);
    init();
    for(int i=0; i<n; i++)
        Insert(str[i]-'a');
    memset(cnt,0,sizeof(cnt));
    memset(c,0,sizeof(c));
    for(State *p=statePool; p!=cur; p++)
        cnt[p->step]++;
    for(int i=1; i<cur-statePool; i++)
        cnt[i]+=cnt[i-1];
    for(State *p=statePool; p!=cur; p++)
        b[--cnt[p->step]]=p;
    for(State *p=statePool; p!=cur; p++)
        p->v=1;

    int num=cur-statePool;
    for(int i=num-1; i>=0; i--)
    {
        State *p=b[i];
        for(int j=0; j<26; j++)
        {
            if(p->go[j])
            {
                int x=p-statePool;
                int y=p->go[j]-statePool;
                son[x][c[x]]=y;
                ch[x][c[x]++]=j+'a';
                p->v+=p->go[j]->v;
            }
        }
    }
    int Q,k;
    scanf("%d",&Q);
    while(Q--)
    {
        scanf("%d",&k);
        Solve(k);
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值