spoj 7258

Lexicographical Substring Search

Time Limit:  1000MSMemory Limit:  Unknown64bit 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("\n");
 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

 

转载于:https://www.cnblogs.com/Mr-ren/p/4215679.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值