• SPOJ SUBLEX


    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("
    ");
    }
    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("
    ");
    }
    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;
    }
  • 相关阅读:
    HDU 1548 A strange lift (Dijkstra)
    HDU 1217 Arbitrage (Floyd)
    HDU 1385 Minimum Transport Cost (Dijstra 最短路)
    考研总结 2016-12-31 20:10 219人阅读 评论(21) 收藏
    归并排序 2016-12-30 20:17 208人阅读 评论(21) 收藏
    docker安装 2016-11-06 19:14 299人阅读 评论(31) 收藏
    Docker初步了解 2016-10-30 20:46 279人阅读 评论(31) 收藏
    [自考]感想 2016-10-23 20:28 261人阅读 评论(32) 收藏
    [自考]C++中一些特殊用法 2016-10-16 22:12 318人阅读 评论(30) 收藏
    Fitnesse批量读取变量信息,并保存到用例执行上下文中
  • 原文地址:https://www.cnblogs.com/zxhl/p/7617446.html
Copyright © 2020-2023  润新知