zoukankan      html  css  js  c++  java
  • D

    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.




    让你求按字典序排名第k名的字符串是什么

    dp处理出每个点往下走能够走出多少个串。
    f[i]=sigma(f[ch[i][c])+1
    这个可以按Max排序之后倒着推就好了。
    询问的时候看一下走下去个数是否<=k,是的话就走下去,然后–k;否则就找下一条边。



     1 #include"bits/stdc++.h"
     2 using namespace std;
     3 const int N = 101000;
     4 #define int long long
     5 struct node
     6 {
     7     int fa;
     8     int ch[26];
     9     int len;
    10 }dian[N<<1];
    11 int cnt[N<<1];
    12 int f[N<<1];
    13 int last=1;int tot=1;
    14 
    15 inline void add(int c)
    16 {
    17   int p=last; int np=last=++tot;
    18   dian[np].len=dian[p].len+1;
    19   for(;p&&!dian[p].ch[c];p=dian[p].fa)dian[p].ch[c]=np;
    20   if(!p) dian[np].fa=1,cnt[1]++;
    21   else
    22   {
    23      int q=dian[p].ch[c];
    24      if(dian[q].len == dian[p].len+1)dian[np].fa=q,cnt[q]++;
    25      else
    26      {
    27          int nq=++tot;
    28          dian[nq]=dian[q];
    29          dian[nq].len=dian[p].len+1;
    30          dian[q].fa=dian[np].fa=nq;
    31 ;         cnt[nq]+=2;
    32          for(;p&&dian[p].ch[c] == q;p=dian[p].fa)dian[p].ch[c]=nq;
    33      }
    34   }
    35 }
    36 
    37 
    38 void dfs(int x)
    39 {
    40     if(f[x]) return ;
    41     f[x]=1;
    42     for(int i=0;i<26;i++)
    43       {
    44            if(dian[x].ch[i])
    45            {dfs(dian[x].ch[i]);
    46             f[x]+= f[dian[x].ch[i]];
    47 
    48            }
    49       }
    50 }
    51 void que()
    52 {
    53     int x;cin>>x; int now=1;
    54     while(x)
    55     {
    56         for(int i=0;i<26;i++)if(dian[now].ch[i])
    57         {
    58             if(f[dian[now].ch[i]]>=x)
    59             {
    60                 putchar('a'+i); --x;now=dian[now].ch[i];
    61                 break;
    62             }
    63             else x-=f[dian[now].ch[i]];
    64         }
    65     }
    66     cout<<'
    ';
    67 }
    68 signed main()
    69 {
    70     //  freopen("datte.txt","r",stdin);
    71    //freopen("my.out","w",stdout);
    72    string s;cin>>s; int len =s.length();
    73     for(int i=0;i<len;i++)add(s[i]-'a');
    74    
    75     dfs(1);  int m;cin>>m;
    76     while(m--)que();
    77 }
  • 相关阅读:
    EF6 Code First 模式更新数据库架构
    bootstrap-datepicker 插件修改为默认中文
    常用网络资源下载
    jQuery框架学习第十一天:实战jQuery表单验证及jQuery自动完成提示插件
    AngularJS实现原理
    [个人翻译]GitHub指导文件(GitHub Guides[Hello World])
    年后跳槽如何准备?
    前端学数据库之子查询
    Ionic实战 自动升级APP(Android版)
    读书笔记:《HTML5开发手册》Web表单
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/10872357.html
Copyright © 2011-2022 走看看