Hdu 5008 (2014 ACM/ICPC Asia Regional Xi & #39; an Online) Boring String Problem (suffix array & amp; binary), hduicpc

Source: Internet
Author: User

Hdu 5008 (2014 ACM/ICPC Asia Regional Xi 'an Online) Boring String Problem (suffix array & Binary), hduicpc
Boring String ProblemTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 219 Accepted Submission (s): 45


Problem DescriptionIn this problem, you are given a string s and q queries.

For each query, you shoshould 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
 
InputThe input consists of multiple test cases. Please process till EOF.

Each test case begins with a line containing a string s (| s | ≤0, 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 shoshould calculate the k by k = (l branch r Branch v) + 1 (l, r is the output of previous question, at the beginning of each case l = r = 0, 0 <k <263, "random" denotes exclusive or)
 
OutputFor 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

aaa40235
 
Sample Output
1 11 31 20 0
 
Source2014 ACM/ICPC Asia Regional Xi 'an Online
Recommendhujie | We have carefully selected several similar problems for you: 5017 5016 5015 5014 question: give you a string of no more than 1 E5. Sort all its substrings by deduplication. Then you need to output the position l, r of the k-th string. If multiple locations have the smallest output l. Idea: The game to see this feeling and SPOJ-7258 Lexicographical Substring Search is very similar. But that's what I saw when I learned about the suffix automatic machine. Because the suffix of the machine is not very easy to understand... So I finally gave up. However, there is a question on the Internet. However, the difference is that the position of the output string is required. So I don't know what to do. So I thought about it with a familiar suffix array. After all, n * log (n) is acceptable. Then you can start it. First we figured out how many different substrings each suffix sa [I] Has than sa [I-1. Obviously, len-sa [I]-height [I] is saved to val [I]. Sa [I] corresponds to the len-sa [I]-height [I] substring. Then, use val [I] to construct a line segment tree. Then, how can I find the k-ranked string. This is similar to the binary idea. If the left subtree of a line segment tree contains more than or equal to k characters, the tree is to the left subtree. If not, go to the right subtree. In this way, we can find the sa [I] corresponding to the k string and The len Length of the k string. After writing it, I got stuck. Because how to deal with l's smallest problem. It is impossible to find sa [I] lcp> = len near I and sa [I] is the smallest. I thought about the situation where all a is the most complex. This degrades to O (n ^ 2. Then sa is written in white. Calm down. Think about it. It's coming out. We can obtain the left position of the leftmost and sa [I] lcp> = len in two ways. Right in the rightmost lcp> = len location of the binary splitting sa [I. Then the answer is the smallest sa in left-> right. This can be maintained using rmq. Then connect to 1A. However, some people are looking for them directly before and after my previous steps. Actually passed. It can be seen that the data is still quite watery. For details, see the code:
#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=100010;typedef long long ll;#define lson L,mid,ls#define rson mid+1,R,rschar txt[maxn];int sa[maxn],T1[maxn],T2[maxn],ct[maxn],he[maxn],rk[maxn],n,m,le,ri;int rmq[25][maxn],lg[maxn],id[25][maxn],pos,len;ll num[maxn<<2];void build(int L,int R,int rt){    if(L==R)    {        num[rt]=n-sa[L]-he[L];        return;    }    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    build(lson);    build(rson);    num[rt]=num[ls]+num[rs];}void getsa(char *st){    int i,k,p,*x=T1,*y=T2;    for(i=0; i<m; i++) ct[i]=0;    for(i=0; i<n; i++) ct[x[i]=st[i]]++;    for(i=1; i<m; i++) ct[i]+=ct[i-1];    for(i=n-1; i>=0; i--)        sa[--ct[x[i]]]=i;    for(k=1,p=1; p<n; k<<=1,m=p)    {        for(p=0,i=n-k; i<n; i++) y[p++]=i;        for(i=0; i<n; i++) if(sa[i]>=k) y[p++]=sa[i]-k;        for(i=0; i<m; i++) ct[i]=0;        for(i=0; i<n; i++) ct[x[y[i]]]++;        for(i=1; i<m; i++) ct[i]+=ct[i-1];        for(i=n-1; i>=0; i--) sa[--ct[x[y[i]]]]=y[i];        for(swap(x,y),p=1,x[sa[0]]=0,i=1; i<n; i++)            x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;    }}void gethe(char *st){    int i,j,k=0;    for(i=0;i<n;i++) rk[sa[i]]=i;    for(i=0;i<n-1;i++)    {        if(k) k--;        j=sa[rk[i]-1];        while(st[i+k]==st[j+k]) k++;        he[rk[i]]=k;    }}void rmq_init(){    int i,j;    for(i=0;i<n;i++)    {        rmq[0][i]=he[i];        id[0][i]=sa[i];    }    for(i=1;i<=lg[n];i++)        for(j=0;j+(1<<i)-1<n;j++)        {            rmq[i][j]=min(rmq[i-1][j],rmq[i-1][j+(1<<(i-1))]);            id[i][j]=min(id[i-1][j],id[i-1][j+(1<<(i-1))]);        }}int rmq_min(int l,int r){    if(l>r)        return 0;    int tmp=lg[r-l+1];    return min(rmq[tmp][l],rmq[tmp][r-(1<<tmp)+1]);}int rmq_id(int l,int r){    int tmp=lg[r-l+1];    return min(id[tmp][l],id[tmp][r-(1<<tmp)+1]);}void prermq(){    int  i;    lg[0]=-1;    for(i=1;i<maxn;i++)        lg[i]=lg[i>>1]+1;}void qu(int L,int R,int rt,ll k){    if(k>num[rt])    {        pos=-1;        le=ri=0;        return;    }    if(L==R)    {        pos=L;        len=he[L]+k;        return;    }    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    if(num[ls]>=k)        qu(lson,k);    else        qu(rson,k-num[ls]);}int binl(int x){    int low=1,hi=x-1,mid,ans=x;    while(low<=hi)    {        mid=(low+hi)>>1;        if(rmq_min(mid+1,x)>=len)            ans=mid,hi=mid-1;        else            low=mid+1;    }    return ans;}int binr(int x){    int low=x+1,hi=n,mid,ans=x;    while(low<=hi)    {        mid=(low+hi)>>1;        if(rmq_min(x+1,mid)>=len)            ans=mid,low=mid+1;        else            hi=mid-1;    }    return ans;}inline ll ReadInt(){    char ch = getchar();    if (ch==EOF) return -1;    ll data = 0;    while (ch < '0' || ch > '9')    {        ch = getchar();        if (ch==EOF) return -1;    }    do    {        data = data*10 + ch-'0';        ch = getchar();    } while (ch >= '0' && ch <= '9');    return data;}inline void putit(int x){    if (x/10>0) putit(x/10);    putchar(x%10+'0');}int main(){    int q,lll,rrr;    ll kth;    prermq();    while(~scanf("%s",txt))    {        m=150,n=strlen(txt);        n++;        getsa(txt);        gethe(txt);        rmq_init();        n--;        build(1,n,1);        scanf("%d",&q);        le=ri=0;        while(q--)        {            kth=ReadInt();            kth=(le^ri^kth)+1;            qu(1,n,1,kth);            if(pos==-1)                putit(le),putchar(' '),putit(ri),putchar('\n');            else            {                lll=binl(pos);                rrr=binr(pos);                le=rmq_id(lll,rrr)+1;                ri=le+len-1;                putit(le),putchar(' '),putit(ri),putchar('\n');            }        }    }    return 0;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.