HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)
题目
Source
http://acm.hdu.edu.cn/showproblem.php?pid=5008
Description
In this problem, you are given a string s and q queries.
For each query, you should answer that when all distinct substrings of string s were sorted lexicographically, which one is the k-th smallest.
A substring si...j of the string s = a1a2 ...an(1 ≤ i ≤ j ≤ n) is the string aiai+1 ...aj. Two substrings sx...y and sz...w are cosidered to be distinct if sx...y ≠ Sz...w
Input
The input consists of multiple test cases.Please process till EOF.
Each test case begins with a line containing a string s(|s| ≤ 105) with only lowercase letters.
Next line contains a postive integer q(1 ≤ q ≤ 105), the number of questions.
q queries are given in the next q lines. Every line contains an integer v. You should calculate the k by k = (l⊕r⊕v)+1(l, r is the output of previous question, at the beginning of each case l = r = 0, 0 < k < 263, “⊕” denotes exclusive or)
Output
For each test case, output consists of q lines, the i-th line contains two integers l, r which is the answer to the i-th query. (The answer l,r satisfies that sl...r is the k-th smallest and if there are several l,r available, ouput l,r which with the smallest l. If there is no l,r satisfied, output “0 0”. Note that s1...n is the whole string)
Sample Input
aaa
4
0
2
3
5
Sample Output
1 1
1 3
1 2
0 0
分析
题目大概说给一个字符串,将所有不同子串从小到大排序,多次询问,每次询问输出第k个子串是哪个子串。
任何一个子串都是某个后缀的前缀,用后缀数组得到所有后缀的排列,然后对于各个后缀i能贡献出的子串就是len-i-height[i]。
可以通过预处理出前缀和,前缀和记录的是前几个后缀贡献的总子串数目;对于各个询问,在前缀和上二分查找,就能得到所的要子串了。
不过,题目还要求输出子串在原串所表示的区间,并且多个方案的情况下输出字典序最小。
那么,可以先找到一个子串S,然后在后缀数组上面再进行两次二分查找,查找到与S的LCP大于等于S长度的后缀rank上界和下界,上界和下界之间所有的子串都是满足要求的,而为了快速找到这个区间字典序最小,用个线段树RMQ一下即可。
代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 111111 int wa[MAXN],wb[MAXN],wv[MAXN],ws[MAXN];
int cmp(int *r,int a,int b,int l){
return r[a]==r[b] && r[a+l]==r[b+l];
}
int sa[MAXN],rnk[MAXN],height[MAXN];
void SA(int *r,int n,int m){
int *x=wa,*y=wb; for(int i=0; i<m; ++i) ws[i]=0;
for(int i=0; i<n; ++i) ++ws[x[i]=r[i]];
for(int i=1; i<m; ++i) ws[i]+=ws[i-1];
for(int i=n-1; i>=0; --i) sa[--ws[x[i]]]=i; int p=1;
for(int j=1; p<n; j<<=1,m=p){
p=0;
for(int i=n-j; i<n; ++i) y[p++]=i;
for(int i=0; i<n; ++i) if(sa[i]>=j) y[p++]=sa[i]-j;
for(int i=0; i<n; ++i) wv[i]=x[y[i]];
for(int i=0; i<m; ++i) ws[i]=0;
for(int i=0; i<n; ++i) ++ws[wv[i]];
for(int i=1; i<m; ++i) ws[i]+=ws[i-1];
for(int i=n-1; i>=0; --i) sa[--ws[wv[i]]]=y[i];
swap(x,y); x[sa[0]]=0; p=1;
for(int i=1; i<n; ++i) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
} for(int i=1; i<n; ++i) rnk[sa[i]]=i;
int k=0;
for(int i=0; i<n-1; height[rnk[i++]]=k){
if(k) --k;
for(int j=sa[rnk[i]-1]; r[i+k]==r[j+k]; ++k);
}
} int n,st[17][MAXN];
void ST(int *a){
for(int i=1; i<=n; ++i) st[0][i]=a[i];
for(int i=1; i<17; ++i){
for(int j=1; j<=n; ++j){
if(j+(1<<i)-1>n) continue;
st[i][j]=min(st[i-1][j],st[i-1][j+(1<<i-1)]);
}
}
}
int logs[MAXN];
int rmq(int a,int b){
int k=logs[b-a+1];
return min(st[k][a],st[k][b-(1<<k)+1]);
}
int lcp(int a,int b){
if(a==b) return n-sa[a];
return rmq(a+1,b);
} int tree[MAXN<<2],N,x,y;
void update(int i,int j,int k){
if(i==j){
tree[k]=y;
return;
}
int mid=i+j>>1;
if(x<=mid) update(i,mid,k<<1);
else update(mid+1,j,k<<1|1);
tree[k]=min(tree[k<<1],tree[k<<1|1]);
}
int query(int i,int j,int k){
if(x<=i && j<=y){
return tree[k];
}
int mid=i+j>>1,ret=MAXN;
if(x<=mid) ret=min(ret,query(i,mid,k<<1));
if(y>mid) ret=min(ret,query(mid+1,j,k<<1|1));
return ret;
} char str[MAXN];
int a[MAXN]; long long sum[MAXN];
int dis[MAXN]; int main(){
for(int i=1; i<MAXN; ++i){
logs[i]=log2(i)+1e-6;
}
while(~scanf("%s",str)){
n=strlen(str);
for(int i=0; i<n; ++i){
a[i]=str[i]-'a'+1;
}
a[n]=0;
SA(a,n+1,28);
ST(height); memset(tree,127,sizeof(tree));
for(N=1; N<n; N<<=1);
for(int i=1; i<=n; ++i){
x=i; y=sa[i];
update(1,N,1);
} for(int i=1; i<=n; ++i){
sum[i]=n-sa[i]-height[i]+sum[i-1];
dis[i]=height[i];
} int q;
long long v,ansl=0,ansr=0;
scanf("%d",&q);
while(q--){
scanf("%I64d",&v);
long long k=(ansl^ansr^v)+1; if(k>sum[n]){
ansl=0; ansr=0;
puts("0 0");
continue;
} int tmp=lower_bound(sum+1,sum+1+n,k)-sum;
int len=dis[tmp]+k-sum[tmp-1]; int l=1,r=tmp;
while(l<r){
int mid=l+r>>1;
if(lcp(mid,tmp)>=len) r=mid;
else l=mid+1;
}
x=l; l=tmp; r=n;
while(l<r){
int mid=l+r+1>>1;
if(lcp(tmp,mid)>=len) l=mid;
else r=mid-1;
}
y=l; ansl=query(1,N,1)+1; ansr=ansl+len-1;
printf("%I64d %I64d\n",ansl,ansr);
}
}
return 0;
}
HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)的更多相关文章
- HDU5008 Boring String Problem(后缀数组)
练习一下字符串,做一下这道题. 首先是关于一个字符串有多少不同子串的问题,串由小到大排起序来应该是按照sa[i]的顺序排出来的产生的. 好像abbacd,排序出来的后缀是这样的 1---abbacd ...
- HDU 5008 Boring String Problem(后缀数组+二分)
题目链接 思路 想到了,但是木写对啊....代码 各种bug,写的乱死了.... 输出最靠前的,比较折腾... #include <cstdio> #include <cstring ...
- K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)
大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...
- [CSP-S模拟测试]:platform(后缀数组+二分+线段树)
题目传送门 题目描述 走过奈何桥有一个名叫望乡台的土台,望乡台有个名曰孟婆的老妇人在卖孟婆汤.一生爱恨情仇,一世浮沉得失,都可以随这碗孟婆汤遗忘得干干净净.现在有$n$碗孟婆汤摆成一排,汤的品种不超过 ...
- HDU - 5008 Boring String Problem (后缀数组+二分法+RMQ)
Problem Description In this problem, you are given a string s and q queries. For each query, you sho ...
- 【BZOJ-4556】字符串 后缀数组+二分+主席树 / 后缀自动机+线段树合并+二分
4556: [Tjoi2016&Heoi2016]字符串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 657 Solved: 274[Su ...
- CF1063F. String Journey(后缀数组+线段树)
题目链接 https://codeforces.com/contest/1063/problem/F 题解 虽然本题有时间复杂度较高但非常好写的做法...... 首先,若答案为 \(k\),则一定存在 ...
- [HEOI2016/TJOI2016]字符串(后缀数组+二分+主席树/后缀自动机+倍增+线段树合并)
后缀数组解法: 先二分最长前缀长度 \(len\),然后从 \(rnk[c]\) 向左右二分 \(l\) 和 \(r\) 使 \([l,r]\) 的 \(height\geq len\),然后在主席树 ...
- 【BZOJ4556】[Tjoi2016&Heoi2016]字符串 后缀数组+二分+主席树+RMQ
[BZOJ4556][Tjoi2016&Heoi2016]字符串 Description 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一 ...
随机推荐
- hdu 1860统计字符
本来是想用map写的,但是map里面会自动按字典序升序排序导致wa了一把,供 #include<time.h> #include <cstdio> #include <i ...
- 造成ORA-12560: TNS: 协议适配器错误的问题的原因有三个:
1.监听服务没有启动 windows平台个一如下操作:开始---程序---管理工具---服务,打开服务面板,启动oraclehome92TNSlistener服务. 2.数据库实例没有启动 windo ...
- php上传文件进度条
ps:本文转自脚本之家 Web应用中常需要提供文件上传的功能.典型的场景包括用户头像上传.相册图片上传等.当需要上传的文件比较大的时候,提供一个显示上传进度的进度条就很有必要了. 在PHP 5.4以前 ...
- Java中length,length(),size()区别
length属性:用于获取数组长度. eg: int ar[] = new int{1,2,3} /** * 数组用length属性取得长度 */ int lenAr = ar.length;//此处 ...
- grep -i 不区分大小写
# rpm -qa|grep -i "mysql" MySQL-server--.rhel5.x86_64 MySQL-test--.rhel5.x86_64 MySQL-embe ...
- 与你相遇好幸运,CentOS 7 x86_64使用Yum安装PostgreSQL
访问http://yum.pgrpms.org/reporpms/repoview/letter_p.group.html,下载并安装和当前系统对应的rpm文件. wget https://downl ...
- eclipse使用tips-Toggle Mark Occurrences 颜色更改
Toggle Mark Occurrences这个功能非常好用,能把选中的方法/变量在本类中全部出现的地方高亮显示,是一个非常实用的功能.但是默认颜色是灰色,非常毁眼.可以通过下面的设置更改为自己喜欢 ...
- 关于strcpy_s
#include"stdafx.h" #include<iostream> #include<cstring> int main() { using nam ...
- BI 项目管理之角色和职责
DW/BI 系统在生命周期中需要许多不同的角色和技能,它们来自业务和技术领域.本文将介绍创建DW/BI 系统所涉及的主要角色.角色和人之间很少是一对一关系.与我们合作的团队小到只有一人,大 ...
- 使用git进行团队合作开发
1.git 和 svn 的差异 git和svn 最大的差异在于git是分布式的管理方式而svn是集中式的管理方式.如果不习惯用代码管理工具,可能比较难理解分布式管理和集中式管理的概念.下面介绍两种工具 ...