看起来像是普通的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. Java中的statickeyword具体解释

    1.statickeyword主要有2个作用: ①为某特定的数据类型或者对象分配单一的存储空间.而与创建对象的个数无关. ②在不创建对象的情况下能够直接通过类名来直接调用方法或者使用类的属性. 2.s ...

  2. Android网络编程http派/申请服务

    最近的研究Android网络编程知识,这里有一些想法,今晚学习.与您分享. 在实际的应用程序的开发非常需要时间appserver请求数据,那么app怎样发送请求呢?以下的代码就是当中的一种情况.使用H ...

  3. 一些周期性GC的理由为何

    1.供tomcat:防止内存泄漏监听器 JreMemoryLeakPreventionListener在上班,每隔一小时默认触发一次System.gc Class clazz = Class.forN ...

  4. WPF界面设计技巧(11)-认知流文档 & 小议WPF的野心

    原文:WPF界面设计技巧(11)-认知流文档 & 小议WPF的野心 流文档是WPF中的一种独特的文档承载格式,它的书写和呈现方式都很像HTML,它也几乎具备了HTML的绝大多数优势,并提供了更 ...

  5. PHP 文件操作类(创建文件并写入) 生成日志

    <?php /** * 文件操作(生成日志)支持多条插入 * (假设插入多条语句并换行 用','逗号分开) * */ class log { public $path = './info.txt ...

  6. e​c​s​h​o​p​模​板​ l​b​i​文​件

    Ecshop根目录/ |->其它目录|->themes |->例:default (模板项目目录) |->images                             ...

  7. JAVA中各种去除空格

    1. String.trim() trim()是去掉首尾空格 2.str.replace(" ", ""); 去掉所有空格,包括首尾.中间 String str ...

  8. oracle看到用户的所有表名、表睐、字段名称、现场的目光、是空的、字段类型

    --oracle看到用户的所有表名.表睐.字段名称.现场的目光.是空的.字段类型 select distinct TABLE_COLUMN.*, TABLE_NALLABLE.DATA_TYPE, T ...

  9. Android自己定义控件系列一:Android怎样实现老版优酷client三级环形菜单

    转载请附上本文链接:http://blog.csdn.net/cyp331203/article/details/40423727 先来看看效果: 一眼看上去好像还挺炫的,感觉比較复杂...实际上并不 ...

  10. Phoenix Framework对于Tree该方法节点设置不同的图标,每个

    在包Javax Swing的Tree对象.我们需要设置不同的图标为每个节点.它封装了一个通用的方法: 用法: jTree1.setCellRenderer(new TreeNodeRender(cas ...