HDU5008 Boring String Problem (后缀数组+二分+RMQ)

https://cn.vjudge.net/problem/HDU-5008

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 s i...j of the string s = a 1a 2 ...a n(1 ≤ i ≤ j ≤ n) is the string aia i+1 ...a j. Two substrings s x...y and s z...w are cosidered to be distinct if sx...y ≠ S z...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| ≤ 10 5) with only lowercase letters. 

Next line contains a postive integer q(1 ≤ q ≤ 10 5), 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 < 2 63, “♁” 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 s l...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 s 1...n is the whole string) 

Sample Input

aaa
4
0
2
3
5

Sample Output

1 1
1 3
1 2
0 0

题意:对于s的全部子串去重并排序,求字典序第k小的子串第一次出现的位置

思路:子串一定是某个后缀的前缀,添进来suffix(sa[i])会新增加len-sa[i]-height[i]个没有出现过的子串(前缀)

由于sa已经是按字典序从小到大排序了,所以这些子串也已经按照字典序从小到大排序了

我们让sum[i]为添完sa[i]后的子串个数(就是个前缀和),找出第一个大于等于k的sum[i]  ,  那么排名为i的后缀一定包含字典序第k小的子串,这个子串的长度为k-sum[i-1]+height[i]

但是这个不一定是第一次出现的位置,包含这个子串的后缀排名一定在i后面,且所有包含这个子串的排名一定是一段连续的区间

二分区间右边界,设包含这个子串的排名区间为[L,R]

再找这些排名中后缀开头的最小位置,即为这个子串的左端点

注意long long

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e5+5;
/*
*suffix array
*倍增算法  O(n*logn)
*待排序数组长度为n,放在0~n-1中,在最后面补一个0
*build_sa( ,n+1, );//注意是n+1;
*getHeight(,n);
*例如:
*n   = 8;
*num[]   = { 1, 1, 2, 1, 1, 1, 1, 2, $ };注意num最后一位为0,其他大于0
*Rank[]  = { 4, 6, 8, 1, 2, 3, 5, 7, 0 };Rank[0~n-1]为有效值,Rank[n]必定为0无效值
*sa[]    = { 8, 3, 4, 5, 0, 6, 1, 7, 2 };sa[1~n]为有效值,sa[0]必定为n是无效值
*height[]= { 0, 0, 3, 2, 3, 1, 2, 0, 1 };height[2~n]为有效值
*
*/
 
int sa[MAXN];//SA数组,表示将S的n个后缀从小到大排序后把排好序的
             //的后缀的开头位置顺次放入SA中
int t1[MAXN],t2[MAXN],c[MAXN];//求SA数组需要的中间变量,不需要赋值
int Rank[MAXN],height[MAXN];
//待排序的字符串放在s数组中,从s[0]到s[n-1],长度为n,且最大值小于m,
//除s[n-1]外的所有s[i]都大于0,r[n-1]=0
//函数结束以后结果放在sa数组中
void build_sa(int s[],int n,int m)
{
    int i,j,p,*x=t1,*y=t2;
    //第一轮基数排序,如果s的最大值很大,可改为快速排序
    for(i=0;i<m;i++)c[i]=0;
    for(i=0;i<n;i++)c[x[i]=s[i]]++;
    for(i=1;i<m;i++)c[i]+=c[i-1];
    for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
    for(j=1;j<=n;j<<=1)
    {
        p=0;
        //直接利用sa数组排序第二关键字
        for(i=n-j;i<n;i++)y[p++]=i;//后面的j个数第二关键字为空的最小
        for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
        //这样数组y保存的就是按照第二关键字排序的结果
        //基数排序第一关键字
        for(i=0;i<m;i++)c[i]=0;
        for(i=0;i<n;i++)c[x[y[i]]]++;
        for(i=1;i<m;i++)c[i]+=c[i-1];
        for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
        //根据sa和x数组计算新的x数组
        swap(x,y);
        p=1;x[sa[0]]=0;
        for(i=1;i<n;i++)
            x[sa[i]]=y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
        if(p>=n)break;
        m=p;//下次基数排序的最大值
    }
}
void getHeight(int s[],int n)
{
    int i,j,k=0;
    for(i=0;i<=n;i++) Rank[sa[i]]=i;
    for(i=0;i<n;i++)
    {
        if(k)k--;
        j=sa[Rank[i]-1];
        while(s[i+k]==s[j+k])k++;
        height[Rank[i]]=k;
    }
}
 
char str[MAXN];
int s[MAXN];
ll sum[MAXN];
int dp[MAXN][20];
void rmq_st_min_height(int n){
	for(int i=1;i<=n;i++) dp[i][0]=height[i];
	int m=log2(1.0*n);
	for(int j=1;j<=m;j++){
		int t=n-(1<<j)+1;
		for(int i=1;i<=t;i++){
			dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
		}
	}
}
int find_min_height(int l,int r){
	int k=log2(1.0*(r-l+1));
	return min(dp[l][k],dp[r-(1<<k)+1][k]);
}
int dpp[MAXN][20];
void rmq_st_min_sa(int n){
	for(int i=1;i<=n;i++) dpp[i][0]=sa[i]+1;
	int m=log2(1.0*n);
	for(int j=1;j<=m;j++){
		int t=n-(1<<j)+1;
		for(int i=1;i<=t;i++){
			dpp[i][j]=min(dpp[i][j-1],dpp[i+(1<<(j-1))][j-1]);
		}
	}
}
int find_min_sa(int l,int r){
	int k=log2(1.0*(r-l+1));
	return min(dpp[l][k],dpp[r-(1<<k)+1][k]);
}
int main()
{
    while(~scanf("%s",str)){
    	int len=strlen(str);
    	for(int i=0;i<=len;i++) s[i]=str[i];
    	build_sa(s,len+1,128);
		getHeight(s,len); 
		rmq_st_min_height(len);
		rmq_st_min_sa(len);
		for(int i=1;i<=len;i++){
			sum[i]=sum[i-1]+len-sa[i]-height[i];
		}
		int q;
		scanf("%d",&q);
		ll l=0,r=0;
		while(q--){
			ll v;
			scanf("%lld",&v);
			ll k=l^r^v;
			k++;
			if(k>sum[len]){
				l=0,r=0;
				printf("%lld %lld\n",l,r);
				continue;
			}
			ll L=1,R=len,mid;
			while(L<=R){
				mid=(L+R)>>1;
				if(sum[mid]<k) L=mid+1;
				else R=mid-1;
			}
			k=k-sum[L-1]+height[L];
			ll lr=L+1,rr=len;
			while(lr<=rr){
				mid=(lr+rr)>>1;
				if(find_min_height(L+1,mid)>=k) lr=mid+1;
				else rr=mid-1;
			}
			R=lr-1;
			l=find_min_sa(L,R);
			r=l+k-1;
			printf("%lld %lld\n",l,r);
		}
	}
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值