zoukankan      html  css  js  c++  java
  • spoj 7258

    Lexicographical Substring Search

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

    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

    题目大意:
    给出一个字符串以及若干个询问.
    求出这个字符串中第K大的字符串.....
     
    思路:
    这个思路非常的明了..
    先统计出字串的个数,然后按照BST的寻找第K大的方法去寻找就ok了....
    常数卡得紧,就下次再优化吧....
      1 #include<cstdlib>
      2 #include<cstdio>
      3 #include<algorithm>
      4 #include<cstring>
      5 using namespace std;
      6 typedef unsigned int uint;
      7 const int maxn = (int)1e5,sigma = 26;
      8 char str[maxn];
      9 int q;
     10 uint k;
     11 int cmp(int,int);
     12 struct Sam{
     13     int ch[maxn * 2][sigma],par[maxn * 2],stp[maxn * 2],id[maxn * 2],sub[maxn * 2];
     14     int sz,last;
     15     void init(){
     16         memset(ch,0,sizeof(ch)); memset(par,0,sizeof(par)); memset(stp,0,sizeof(stp));
     17         sz = last = 1;
     18     }
     19     void ext(int c){
     20         stp[++sz] = stp[last] + 1;
     21         int p = last, np = sz;
     22         for(; !ch[p][c]; p = par[p]) ch[p][c] = np;
     23         if(p == 0) par[np] = 1;
     24         else{
     25             int q = ch[p][c];
     26             if(stp[q] != stp[p] + 1){
     27                 stp[++sz] = stp[p] + 1;
     28                 int nq = sz;
     29                 memcpy(ch[nq],ch[q],sizeof(ch[q]));
     30                 par[nq] = par[q];
     31                 par[q] = par[np] = nq;
     32                 for(; ch[p][c] == q; p = par[p]) ch[p][c] = nq;
     33             }
     34             else
     35                 par[np] = q;
     36         }
     37         last = np;
     38     }
     39     void ins(char *pt){
     40         int i;
     41         init();
     42         for(i = 0; pt[i]; ++i) ext(pt[i] - 'a');
     43     }
     44     void prep(){
     45         int i,j;
     46         for(i = 1; i <= sz; ++i) id[i] = i;
     47         sort(id + 1, id + sz + 1, cmp);
     48         for(i = 1; i <= sz; ++i) sub[i] = 1;
     49         for(i = 1; i <= sz; ++i)
     50             for(j = 0; j < sigma; ++j)
     51                 if(ch[id[i]][j]) sub[id[i]] += sub[ch[id[i]][j]];
     52     }
     53     void solve(uint k){
     54         int i,x = 1;
     55         while(k){
     56             for(i = 0; i < sigma; ++i)
     57                 if(ch[x][i] && k <= sub[ch[x][i]]){
     58                     printf("%c",i + 'a');
     59                     x = ch[x][i];
     60                     k--;
     61                     break;
     62                 }
     63                 else k -= sub[ch[x][i]];            
     64         }
     65         printf("
    ");
     66     }
     67 }sam;
     68 int cmp(int x,int y){
     69     return sam.stp[x] > sam.stp[y];
     70 }
     71 int main()
     72 {
     73     freopen("sublex.in","r",stdin);
     74     freopen("sublex.out","w",stdout);
     75     scanf("%s",str);
     76     sam.ins(str);
     77     sam.prep();
     78     scanf("%d",&q);
     79     while(q--){
     80         scanf("%u",&k);
     81         sam.solve(k);
     82     }
     83     return 0;
     84 } 
     85 
     86 int cmp(int x,int y){
     87     return sam.stp[x] > sam.stp[y];
     88 }
     89 int main()
     90 {
     91     scanf("%s",str);
     92     sam.ins(str);
     93     sam.prep();
     94     scanf("%d",&q);
     95     while(q--){
     96         scanf("%u",&k);
     97         sam.solve(k);
     98     }
     99     return 0;
    100 } 
    View Code
  • 相关阅读:
    bzoj 2038 [2009国家集训队]小Z的袜子(hose)
    【NOIP2014模拟11.1B组】吴传之火烧连营
    【NOIP2014模拟11.1B组】蜀传之单刀赴会
    phpmystudy:mysql启动失败
    英文漏洞报告解读(一)——PHP 5.4.x < 5.4.32 Multiple Vulnerabilities
    brupsuit Compare 模块及其应用场景
    Android实现Banner界面广告图片循环轮播(包括实现手动滑动循环)
    android ViewPager实现的轮播图广告自定义视图,网络获取图片和数据
    Android首页轮播图直接拿来用
    java 调用webservice的各种方法总结
  • 原文地址:https://www.cnblogs.com/Mr-ren/p/4215679.html
Copyright © 2011-2022 走看看