1. 程式人生 > >SPOJ SUBLEX - Lexicographical Substring Search 後綴自動機 / 後綴數組

SPOJ SUBLEX - Lexicographical Substring Search 後綴自動機 / 後綴數組

-a for 一個 state cond strlen test nal ttl

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.

題意:

  給你一個串

  Q個詢問,在其去重子串中,字典序排名為K的字串是哪一個 ,並輸出來

題解:

  說一下後綴自動機的做法

  設定F[i] 表示 以狀態i為起點 所能 形成的不同字串的個數

  求出來,再類似於貪心的找法求出答案串

  後綴數組做更簡單些

後綴自動機

#include <bits/stdc++.h>
inline long long read(){long long x=0,f=1;char ch=getchar();while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}return x*f;}
using namespace std;

const int N = 3e5+7;

const long long mod = 1000000007;

int isPlus[N * 2],endpos[N * 2];int d[N * 2];
int tot,slink[2*N],trans[2*N][28],minlen[2*N],maxlen[2*N],pre;
int newstate(int _maxlen,int _minlen,int* _trans,int _slink){
    maxlen[++tot]=_maxlen;minlen[tot]=_minlen;
    slink[tot]=_slink;
    if(_trans)for(int i=0;i<26;i++)trans[tot][i]=_trans[i],d[_trans[i]]+=1;
    return tot;
}
int add_char(char ch,int u){
    int c=ch-a,v=u;
    int z=newstate(maxlen[u]+1,-1,NULL,0);
    isPlus[z] = 1;
    while(v&&!trans[v][c]){trans[v][c]=z;d[z]+=1;v=slink[v];}
    if(!v){ minlen[z]=1;slink[z]=1;return z;}
    int x=trans[v][c];
    if(maxlen[v]+1==maxlen[x]){slink[z]=x;minlen[z]=maxlen[x]+1;return z;}
    int y=newstate(maxlen[v]+1,-1,trans[x],slink[x]);
    slink[z]=slink[x]=y;minlen[x]=minlen[z]=maxlen[y]+1;
    while(v&&trans[v][c]==x){trans[v][c]=y;d[x]--,d[y]++;v=slink[v];}
    minlen[y]=maxlen[slink[y]]+1;
    return z;
}
void init_sam() {
    for(int i = 1; i <= tot; ++i)
        for(int j = 0; j < 26; ++j) trans[i][j] = 0;
    pre = tot = 1;
}
long long f[N],all[N];
char a[N];
int cnt[N],pos[N];
void query(long long k) {
    int p = 1;
    while(k) {
        long long now = 0;
        for(int i = 0; i < 26; ++i) {
            if(!trans[p][i]) continue;
            if(f[trans[p][i]] >= k) {
                k--;
                p = trans[p][i];
                printf("%c",i+a);
                break;
            }
            else k -= f[trans[p][i]];
        }
    }
    printf("\n");
}
int main() {
    scanf("%s",a);
    int n = strlen(a);
    init_sam();
    for(int i = 0; i < n; ++i)
        pre = add_char(a[i],pre);
    for(int i = 0; i <= n; ++i) cnt[i] = 0;
    for(int i = 1; i <= tot; ++i) cnt[maxlen[i]]++,all[i] = maxlen[i] - minlen[i] + 1;
    for(int i = 1; i <= n; ++i) cnt[i] += cnt[i-1];
    for(int i = tot; i >= 1; --i) pos[cnt[maxlen[i]]--] = i;

    //for(int i = 2; i <= tot; ++i) cout<<all[i]<<" "<<slink[i]<<endl;

    for(int i = tot; i >= 2; --i) {
        int v = pos[i];
        f[v] = 1;
        for(int j = 0; j < 26; ++j) {
            f[v] += f[trans[v][j]];
        }
    }
    int Q;
    scanf("%d",&Q);
    while(Q--) {
        long long k;
        scanf("%lld",&k);
        query(k);
    }
    return 0;
}

後綴數組

#include <bits/stdc++.h>
inline long long read(){long long x=0,f=1;char ch=getchar();while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}return x*f;}
using namespace std;

const int N = 3e5+7;

const long long mod = 1000000007;

int *ran,r[N],sa[N],height[N],wa[N],wb[N],wm[N];
bool cmp(int *r,int a,int b,int l) {
    return r[a] == r[b] && r[a+l] == r[b+l];
}
void SA(int *r,int *sa,int n,int m) {
    int *x=wa,*y=wb,*t;
    for(int i=0;i<m;++i)wm[i]=0;
    for(int i=0;i<n;++i)wm[x[i]=r[i]]++;
    for(int i=1;i<m;++i)wm[i]+=wm[i-1];
    for(int i=n-1;i>=0;--i)sa[--wm[x[i]]]=i;
    for(int i=0,j=1,p=0;p<n;j=j*2,m=p){
        for(p=0,i=n-j;i<n;++i)y[p++]=i;
        for(i=0;i<n;++i)if(sa[i]>=j)y[p++]=sa[i]-j;
        for(i=0;i<m;++i)wm[i]=0;
        for(i=0;i<n;++i)wm[x[y[i]]]++;
        for(i=1;i<m;++i)wm[i]+=wm[i-1];
        for(i=n-1;i>=0;--i)sa[--wm[x[y[i]]]]=y[i];
        for(t=x,x=y,y=t,i=p=1,x[sa[0]]=0;i<n;++i) {
            x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
        }
    }
    ran=x;
}
void Height(int *r,int *sa,int n) {
    for(int i=0,j=0,k=0;i<n;height[ran[i++]]=k)
    for(k?--k:0,j=sa[ran[i]-1];r[i+k] == r[j+k];++k);
}


int n;
long long f[N];
char a[N];
void query(long long k) {
    int pos = lower_bound(f+1,f+n+1,k) - f;
    long long lll = k - f[pos-1] + height[pos];
    for(int i = sa[pos]; i < sa[pos]+lll; ++i)
        printf("%c",a[i]);
        printf("\n");
}
int main() {
    scanf("%s",a);
    n = strlen(a);
    for(int i = 0; i < n; ++i) r[i] = a[i] - a+1;
    r[n] = 0;
    SA(r,sa,n+1,300);
    Height(r,sa,n);
    for(int i = 1; i <= n; ++i)
        f[i] = f[i-1] + n - sa[i] - height[i];

    int Q;
    long long k;
    scanf("%d",&Q);
    while(Q--) {
        scanf("%lld",&k);
        query(k);
    }
    return 0;
}

SPOJ SUBLEX - Lexicographical Substring Search 後綴自動機 / 後綴數組