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

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

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

[Submit]   [Go Back]  
[

id=28015" style="color:blue; text-decoration:none">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

[Submit]   [Go Back]  
[

id=28015" style="color:blue; text-decoration:none">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;
}

SPOJ SUBLEX 7258. Lexicographical Substring Search的更多相关文章

  1. 【SPOJ】7258. Lexicographical Substring Search(后缀自动机)

    http://www.spoj.com/problems/SUBLEX/ 后缀自动机系列完成QAQ...撒花..明天or今晚写个小结? 首先得知道:后缀自动机中,root出发到任意一个状态的路径对应一 ...

  2. 【spoj SUBLEX】 Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ (题目链接) 题意 给出一个字符串,询问其中字典序第K小的子串. Solution 后缀自动机例题. 构出后缀自动机以后,对每 ...

  3. 【SPOJ - SUBLEX】Lexicographical Substring Search 【后缀自动机+dp】

    题意 给出一个字符串和q个询问,每个询问给出一个整数k,输出第k大得子串. 分析 建后缀自动机,利用匹配边来解决.设d[v]为从状态v开始有多少不同的路径.这个显然是可以递推出来的.然后对于每个询问, ...

  4. spoj 7258 Lexicographical Substring Search (后缀自动机)

    spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...

  5. SPOJ 7258 Lexicographical Substring Search(后缀自动机)

    [题目链接] http://www.spoj.com/problems/SUBLEX/ [题目大意] 给出一个字符串,求其字典序排名第k的子串 [题解] 求出sam上每个节点被经过的次数,然后采用权值 ...

  6. ●SPOJ 7258 Lexicographical Substring Search

    题链: http://www.spoj.com/problems/SUBLEX/题解: 后缀自动机. 首先,因为相同的子串都被存在了自动机的同一个状态里面,所以这就很自然的避免了重复子串的问题. 然后 ...

  7. SPOJ 7258 Lexicographical Substring Search

    Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...

  8. SPOJ 7258 Lexicographical Substring Search [后缀自动机 DP]

    题意:给一个长度不超过90000的串S,每次询问它的所有不同子串中,字典序第K小的,询问不超过500个. 第一道自己做的1A的SAM啦啦啦 很简单,建SAM后跑kth就行了 也需要按val基数排序倒着 ...

  9. SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组

    SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...

随机推荐

  1. hdu2126(求方案数的01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 题意: n个物品,m元钱,每个物品最多买一次,问最多可以买几件物品,并且输出方案数. 分析:一看 ...

  2. Unity MVC框架 StrangeIoC

    StrangeIoC是一个超轻量级和高度可扩展的控制反转(IoC)框架,专门为C#和Unity编写. 项目地址:https://github.com/strangeioc/strangeioc 文档地 ...

  3. Keepalived+LVS+Nginx负载均衡之高可用

    Keepalived+LVS+Nginx负载均衡之高可用 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常 ...

  4. 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...

  5. const使用摘要

    const在四种方案如以下: int b = 500; const int *a = &b; ①(底层const) int const *a = &b; ②(底层const) int ...

  6. hdu1005 Number Sequence(寻找循环节)

    主题链接: pid=1005">huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力 ...

  7. IM设计与实现的系统模块的聊天记录

    到非常多开发IM系统的朋友都想实现聊天记录存储和查询这一不可或缺的功能,这里我就把自己前段时间为傲瑞通(OrayTalk)开发聊天记录模块的经验分享出来,供须要的朋友參考下. 一.整体设计 1.存储位 ...

  8. 【Java基础】异常的简单分类与处理

    Java中所有的异常都继承自Throwable类,Throwable类的已知子类有Error和Exception. Error是指系统出现的错误,这种错误出现的时候,我们的程序无能为力,所以不需要进行 ...

  9. [033] 微信公众帐号开发教程第9篇-QQ表情的发送与接收

    我想大家对QQ表情一定不会陌生,一个个小头像极大丰富了聊天的乐趣,使得聊天不再是简单的文字叙述,还能够配上喜.怒.哀.乐等表达人物心情的小图片.本文重点要介绍的内容就是如何在微信公众平台使用QQ表情, ...

  10. 检验身份证的正确性(Golang版本)

    // CheckID_card project main.go package main /* * ai -> a1 , a2, a3, a4, a5, a6... a17 (a18 是校验码) ...