看起来像是普通的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. vs2008+opencv2.4.9 +win7X64位系统 2.

    小编用自身血淋淋的例子,来给大家做个参考,共耗时近2天时间,终于屈服于安装vs2010,然后配置成功了.但是在这个配置成功后,我终于发现了我08配置不成功的原因,写下心得,供各位参考 1.准备工具 v ...

  2. 【Oracle】-【sqlplus / as sysdba登录报错问题】-新用户使用sqlplus / as sysdba登录报错

    刚才打开一个别人的测试库,用root登陆了的,sqlplus / as sysdba竟然报错,奇怪,于是在自己的VM中模拟该过程. 新建了一个test用户: [test@liu bin]# ./sql ...

  3. 多线程之join方法

    join方法的功能就是使异步执行的线程变成同步执行.也就是说,当调用线程实例的start方法后,这个方法会立即返回,如果在调用start方法后后需要使用一个由这个线程计算得到的值,就必须使用join方 ...

  4. adt-bundle-windows-x86_32-20140702

    adt-bundle-windows-x86_32-20140702 Windows > Preference,在Android里没有NDK选项 法一:help->install new ...

  5. pygame系列_mouse鼠标事件

    pygame.mouse提供了一些方法获取鼠标设备当前的状态 ''' pygame.mouse.get_pressed - get the state of the mouse buttons get ...

  6. Windows下Putty连接虚拟机Ubuntu

    本文的题目是Windows下使用Putty连接虚拟机中的Ubuntu. 事实上针对这种一个问题,已经有非常多的文章.blog能够參考和学习.可是在本人的学习过程中还是遇到可非常多的问题. 特写下自己的 ...

  7. js中 正則表達式

    正則表達式使用具体解释 简单介绍 简单的说,正則表達式是一种能够用于模式匹配和替换的强有力的工具.其作用例如以下: 測试字符串的某个模式.比如,能够对一个输入字符串进行測试,看在该字符串是否存在一个电 ...

  8. STM32W108无线传感器网络嵌入式uCOS-II的移植及实时环境监測

    基于STM32W108无线开发板,将ucos-ii v2.86内核移植到其上,并加入用户任务.实现对温湿度.超声波.声音.光敏等传感器的控制及实时数据採集. 14.1开发环境说明 硬件:STM32W1 ...

  9. 足球和oracle系列(3):oracle过程排名,世界杯第二回合战罢到来!

    足球与oracle系列(3):oracle进程排名.世界杯次回合即将战罢! 声明:        这不是技术文档,既然学来几招oracle简单招式.就忍不了在人前卖弄几下.纯为茶余饭后与数朋库友的插科 ...

  10. Galaxy (hdu 5073 数学)

    Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total S ...