zoukankan      html  css  js  c++  java
  • SPOJ 7258 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

    题解:
    这题磨人啊,SPOJ跑的又慢,时限又短,压了一晚上常。
    思路如下:
    SAM满足性质:dfs序每一步走出来的都是一个子串,且满足从小到大顺序,关键满足不重不漏.
    那么根据性质可以想出:沿着SAM走k步就是k-th子串了.
    考虑优化:
    记录size[x]为x节点往下可以产生的子串个数,和上题类似可以拓扑序搞出来 然后如果小于rk那么就直接减去即可.
    压常技巧:
    1.rg inline 不解释
    2.不知为何,代码中k的输入写在函数内就快了4倍 懂得可以评论下....
     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #define RG register
     8 using namespace std;
     9 const int N=90005,M=300005;
    10 char s[M];int n=0,cnt=1,last=1,cur=1,fa[M],ch[M][27],dis[M],size[M];
    11 void build(int c){
    12     last=cur;cur=++cnt;
    13     int p=last;
    14     dis[cur]=++n;
    15     for(;p && !ch[p][c];p=fa[p])ch[p][c]=cur;
    16     if(!p)fa[cur]=1;
    17     else{
    18         int q=ch[p][c];
    19         if(dis[q]==dis[p]+1)fa[cur]=q;
    20         else{
    21             int nt=++cnt;
    22             dis[nt]=dis[p]+1;
    23             memcpy(ch[nt],ch[q],sizeof(ch[q]));
    24             fa[nt]=fa[q];fa[q]=fa[cur]=nt;
    25             for(;ch[p][c]==q;p=fa[p])ch[p][c]=nt;
    26         }
    27     }
    28 }
    29 int sa[M];int c[M];
    30 void Flr(){
    31     int p;
    32     for(RG int i=1;i<=cnt;i++)c[dis[i]]++;
    33     for(RG int i=1;i<=n;i++)c[i]+=c[i-1];
    34     for(RG int i=cnt;i;i--)sa[c[dis[i]]--]=i;
    35     for(RG int i=cnt;i;i--){
    36         p=sa[i];
    37         size[p]=1;
    38         for(int j=0;j<=25;j++)
    39         size[p]+=size[ch[p][j]];
    40     }
    41 }
    42 void dfs(){
    43     RG int u,x=1,rk;
    44     scanf("%d",&rk);
    45     while(rk)
    46     {
    47         for(int i=0;i<=25;i++){
    48             u=ch[x][i];
    49             if(!u)continue;
    50             if(size[u]>=rk){
    51                 putchar('a'+i);
    52                 x=u;rk--;break;
    53             }
    54              else rk-=size[u];
    55         }
    56     }
    57     puts("");
    58 }
    59 void work(){
    60     scanf("%s",s);
    61     for(RG int i=0,l=strlen(s);i<l;i++)build(s[i]-'a');
    62     Flr();
    63     int Q,x;
    64     scanf("%d",&Q);
    65     while(Q--)dfs();
    66 }
    67 int main()
    68 {
    69     work();
    70     return 0;
    71 }
    
    
    


  • 相关阅读:
    linux系统根目录文件系统空间不足导致的错误
    python---对象
    公共函数
    PHP接口(interface)和抽象类(abstract)
    mysql引擎
    InstallShield自定义图片资源
    InstallShield 创建自己的Dialog
    InstallShield:自己备份
    注册表和ODBC
    IS脚本学习
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7271794.html
Copyright © 2011-2022 走看看