zoukankan      html  css  js  c++  java
  • SPOJ SUBLEX 7258. Lexicographical Substring Search


    看起来像是普通的SAM+dfs...但SPOJ太慢了......倒腾了一个晚上不是WA 就是RE .....

    最后换SA写了......


    Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

    []   [Go Back]   [Status]  

    Description

    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
    

    Source

    Own Problem

    []   [Go Back]   [Status]  




    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn=99000;
    
    int sa[maxn],rank[maxn],rank2[maxn],h[maxn],c[maxn];
    int *x,*y,ans[maxn];
    char str[maxn];
    
    bool cmp(int*r,int a,int b,int l,int n)
    {
        if(r[a]==r[b]&&a+l<n&&b+l<n&&r[a+l]==r[b+l])
            return true;
        return false;
    }
    
    void radix_sort(int n,int sz)
    {
        for(int i=0;i<sz;i++) c[i]=0;
        for(int i=0;i<n;i++) c[x[y[i]]]++;
        for(int i=1;i<sz;i++) c[i]+=c[i-1];
        for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
    }
    
    void get_sa(char c[],int n,int sz=128)
    {
        x=rank,y=rank2;
        for(int i=0;i<n;i++)
            x[i]=c[i],y[i]=i;
        radix_sort(n,sz);
        for(int len=1;len<n;len=len*2)
        {
            int yid=0;
            for(int i=n-len;i<n;i++)
                y[yid++]=i;
            for(int i=0;i<n;i++)
                if(sa[i]>=len)
                    y[yid++]=sa[i]-len;
    
            radix_sort(n,sz);
    
            swap(x,y);
            x[sa[0]]=yid=0;
    
            for(int i=1;i<n;i++)
                x[sa[i]]=cmp(y,sa[i],sa[i-1],len,n)?yid:++yid;
    
            sz=yid+1;
            if(sz>=n) break;
        }
        for(int i=0;i<n;i++)
            rank[i]=x[i];
    }
    
    void get_h(char str[],int n)
    {
        int k=0; h[0]=0;
        for(int i=0;i<n;i++)
        {
            if(rank[i]==0) continue;
            k=max(k-1,0);
            int j=sa[rank[i]-1];
            while(i+k<n&&j+k<n&&str[i+k]==str[j+k])
                k++;
            h[rank[i]]=k;
        }
    }
    
    int sum[maxn];
    
    int main()
    {
        scanf("%s",str);
        int n=strlen(str);
        get_sa(str,n);
        get_h(str,n);
    
        for(int i=0;i<n;i++)
        {
            sum[i]=n-sa[i]-h[i];
            if(i-1>=0) sum[i]+=sum[i-1];
        }
    
        int T_T;
        scanf("%d",&T_T);
        while(T_T--)
        {
            int x;
            scanf("%d",&x);
            int low=0,high=n-1,ans,mid;
            while(low<=high)
            {
                mid=(low+high)/2;
                if(sum[mid]>=x)
                    ans=mid,high=mid-1;
                else if(sum[mid]<x) low=mid+1;
                else break;
            }
            int t=(ans==0)?

    0:sum[ans-1]; for(int i=0;i<h[ans];i++) putchar(str[sa[ans]+i]); for(int i=0;i<x-t;i++) putchar(str[sa[ans]+h[ans]+i]); putchar(10); } return 0; }




  • 相关阅读:
    UNI-APP相关笔记
    解决了非华为手机无法使用新版本《心脏健康研究(com.plagh.heartstudy)》APP的问题
    HTML5使用纯CSS实现“按比例平分”整个垂直空间
    ZUK Z2 AospExtended-v6.7 Android 9.0可用的谷歌相机Mod.md
    git revert 和 git reset的区别
    python安装
    eclipse集成python插件
    eclipse 编辑 python 中文乱码的解决方案
    性能测试第八章学习 Loadrunner http POST三种请求格式的脚本
    性能测试第七章-Loadrunner参数化
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5093160.html
Copyright © 2011-2022 走看看