HDU 5008 B - Boring String Problem(非常好的题目)

Boring String Problem

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1798 Accepted Submission(s): 479

Problem 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小串并输出最小的下标。

吐槽:自己真是很弱鸡啊,比赛的时候没有控制好最小下标这一条,一直WA。

首先我们知道在sa[i]的后缀 我们 可以得到 len-sa[i]-height[i]个子串。之后我们用一个数组存下载当前位置所以子串个数之和。

之后得到k后如果K>sum[len]时一定为0。

之后我们用二分的方法找到 第k小串最近的那个串的下标。

之后我们可以得到 该第k小串的长度。sublen(具体看代码)

之后我们可以从该串的下一个下标开始通过height[ ]找到他的最小下标。

之后右边为 l+sublen-1;

因为是题目中以 1-》n 那么我们在最后要把r l 都加1。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#define maxs 100200
#define MME(i,j) memset(i,j,sizeof(i))
using namespace std;
long long templen,v,l=0,r=0;
unsigned long long k;
int s[maxs],sa[maxs],ranks[maxs],height[maxs];
int wa[maxs],wb[maxs],wd[maxs],wv[maxs];
char input[maxs];
long long  son[maxs],len;
int cmp(int *r,int a,int b,int k){

    return r[a]==r[b]&&r[a+k]==r[b+k];
}

void get_sa(int *r,int n,int m)
{
    int i,j,p,*x=wa,*y=wb;
    for(i=0;i<m;i++) wd[i]=0;
    for(i=0;i<n;i++) wd[x[i]=r[i]]++;
    for(i=1;i<m;i++) wd[i]+=wd[i-1];
    for(i=n-1;i>=0;i--) sa[--wd[x[i]]]=i;

    for(j=1,p=1;p<n;j*=2,m=p)
    {
        for(p=0,i=n-j;i<n;i++) y[p++]=i;
        for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
        for(i=0;i<n;i++) wv[i]=x[y[i]];

        for(i=0;i<m;i++) wd[i]=0;
        for(i=0;i<n;i++) wd[wv[i]]++;
        for(i=1;i<m;i++) wd[i]+=wd[i-1];

        for(i=n-1;i>=0;i--) sa[--wd[wv[i]]]=y[i];
        for(swap(x,y),p=1,x[sa[0]]=0,i=1;i<n;i++){

            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
    }
   /*for(int i=0;i<n;i++)
       printf("sa[%d] is %d\n",i,sa[i]);*/
}

void build_height(int *r,int n)
{
    int i,j,k=0;
    for(i=1;i<=n;i++) ranks[sa[i]]=i;
    for(i=0;i<n;height[ranks[i++]]=k)
    {
        for(k?k--:0,j=sa[ranks[i]-1];r[i+k]==r[j+k];k++);
    }
    /*for(int i=0;i<=n;i++)
       printf("H[%d] is %d\n",i,height[i]);*/
}


long long mins(long long x,long long y)
{
    return x<y?x:y;
}
int main()
{
    while(~scanf("%s",input))
    {
        l=r=0;
        len=strlen(input);
        for(int i=0;i<len;i++)
        {
            s[i]=input[i]-'a'+1;
        }
        s[len]=0;
        get_sa(s,len+1,28);
        build_height(s,len);
        son[0]=0;
        for(int i=1;i<=len;i++)
        {
            son[i]=son[i-1]+len-sa[i]-height[i];
        }
      /*  LCP_init(height,len);
        RMQ_init(sa,len);*/
        int q;
        scanf("%d",&q);

        for(int i=0;i<q;i++)
        {

            scanf("%lld",&v);
            k=(l^r^v)+1;

            if(k>son[len])
            {
                l=r=0;
                printf("0 0\n");
                continue;
            }
            int pos;
            int L=0,R=len,mid;
            while(R>=L)
            {
                mid=(L+R)>>1;
                if(son[mid]<k)
                {
                    pos=mid;
                    L=mid+1;
                }
                else
                    R=mid-1;
            }//找到前一个下标
            int sublen;
            sublen=height[pos+1]+k-son[pos];//第K小串的长度

            l=sa[pos+1],pos+=2;//第K小串后一个后缀的下标

            while(height[pos]>=sublen)
            {
                l=mins(l,sa[pos]),pos++;
            }//找到最近的左下标

            r=l+sublen-1;

            l=l+1;
            r=r+1;
            printf("%lld %lld\n",l,r);
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值