SPOJ SUBLEX 7258. Lexicographical Substring Search
看起来像是普通的SAM+dfs...但SPOJ太慢了......倒腾了一个晚上不是WA 就是RE .....
最后换SA写了......
[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 S = "aaa" (without quotes) InputIn 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 OutputOutput consists of Q lines, the i-th contains a string which is the answer to the i-th asked question. ExampleInput: 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的更多相关文章
- 【SPOJ】7258. Lexicographical Substring Search(后缀自动机)
http://www.spoj.com/problems/SUBLEX/ 后缀自动机系列完成QAQ...撒花..明天or今晚写个小结? 首先得知道:后缀自动机中,root出发到任意一个状态的路径对应一 ...
- 【spoj SUBLEX】 Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ (题目链接) 题意 给出一个字符串,询问其中字典序第K小的子串. Solution 后缀自动机例题. 构出后缀自动机以后,对每 ...
- 【SPOJ - SUBLEX】Lexicographical Substring Search 【后缀自动机+dp】
题意 给出一个字符串和q个询问,每个询问给出一个整数k,输出第k大得子串. 分析 建后缀自动机,利用匹配边来解决.设d[v]为从状态v开始有多少不同的路径.这个显然是可以递推出来的.然后对于每个询问, ...
- spoj 7258 Lexicographical Substring Search (后缀自动机)
spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...
- SPOJ 7258 Lexicographical Substring Search(后缀自动机)
[题目链接] http://www.spoj.com/problems/SUBLEX/ [题目大意] 给出一个字符串,求其字典序排名第k的子串 [题解] 求出sam上每个节点被经过的次数,然后采用权值 ...
- ●SPOJ 7258 Lexicographical Substring Search
题链: http://www.spoj.com/problems/SUBLEX/题解: 后缀自动机. 首先,因为相同的子串都被存在了自动机的同一个状态里面,所以这就很自然的避免了重复子串的问题. 然后 ...
- SPOJ 7258 Lexicographical Substring Search
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- SPOJ 7258 Lexicographical Substring Search [后缀自动机 DP]
题意:给一个长度不超过90000的串S,每次询问它的所有不同子串中,字典序第K小的,询问不超过500个. 第一道自己做的1A的SAM啦啦啦 很简单,建SAM后跑kth就行了 也需要按val基数排序倒着 ...
- SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组
SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...
随机推荐
- AJAX POST请求中參数以form data和request payload形式在servlet中的获取方式
HTTP请求中,假设是get请求,那么表单參数以name=value&name1=value1的形式附到url的后面,假设是post请求,那么表单參数是在请求体中,也是以name=value& ...
- OpenStack安装与配置2
第二部分 OpenStack安装与配置 一.引言 本章内容讲解如何在3台物理机上搭建最小化云平台,这3台机器分为称为Server1.Server2和Client1,之后的各章也是如此.Server ...
- .net数据根据字段进行分类(linq语句)
var items = List<实体>; var models = items.GroupBy(r => r.分类字段).ToDictionary(d => d.Key, d ...
- SSM框架理解(转)
SSM框架理解 最近两星期一直在学JavaEE的MVC框架,因为之前学校开的JavaEE课程就一直学的吊儿郎当的,所以现在真正需要掌握就非常手忙脚乱,在此记录下这段时间学习的感悟,如有错误,希望大牛毫 ...
- .NET开源 FAQ
Microsoft至2014年11月12日本(PST)公布.NET开源.一个"隐居"商业帝国也迎来"改革开放".. . Q1:为什么要开放源码? Ans:由于. ...
- 二叉树的建立与遍历(山东理工OJ)
题目描写叙述 已知一个按先序序列输入的字符序列,如abc,,de,g,,f,,,(当中逗号表示空节点).请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度. 输入 输入一个长度 ...
- Java 并发编程(三)为线程安全类中加入新的原子操作
Java 类库中包括很多实用的"基础模块"类.通常,我们应该优先选择重用这些现有的类而不是创建新的类.:重用能减少开发工作量.开发风险(由于现有类都已经通过測试)以及维护成本.有时 ...
- 杭州电 3711 Binary Number
Binary Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 简单ESB的服务架构
简单ESB的服务架构 这几个月一直在修改架构,所以迟迟没有更新博客. 新的架构是一个基于简单esb的服务架构,主要构成是esb服务注册,wcf服务,MVC项目构成. 首先,我门来看一看解决方案, 1. ...
- MySQL 批量Dll操作(转)
概述 本章节介绍使用游标来批量进行表操作,包括批量添加索引.批量添加字段等.如果对存储过程.变量定义.预处理还不是很熟悉先阅读我前面写过的关于这三个概念的文章,只有先了解了这三个概念才能更好的理解这篇 ...
