poj1743 suffix array + two-point answer

Source: Internet
Author: User

1. Given a string, find the longest repeating substring, and the two substrings can overlap.
This problem is a simple application of the suffix array. The procedure is simple, just require the height array
The maximum value in the
2. Given a string, to find the longest repeating substring, these two substrings cannot overlap.
This question is slightly more complicated than the previous one. The first two points, the question into a decision-making problem: judge whether
There are two substrings of length k that are the same and do not overlap. The key to solving this problem is to use
The height array. Divides the sorted suffix into groups, where the height value between the suffixes of each group is
Not less than K. For example, the string is "Aabaaaab", when k=2, the suffix is divided into 4 groups, 5

is shown.

It is easy to see that there is a hope that the longest common prefix of two suffixes not less than k must be in the same group. Then
For each set of suffixes, only the difference between the maximum and minimum values of the SA value for each suffix is not less than
K. If there is a set of satisfies, the description exists, otherwise it does not exist.

/*own ideas: Just beginning to see the topic has been tangled how to make add or subtract, and then a think if 2 to want to die, then the difference between the next one and the other end must be the same. So the value is processed first, and then an array is obtained, which obviously asks for height[]. I have been thinking about how to do it, but I can't think of it.    Later read the paper, two answers, to find the maximum value. Say the ordinary point is K this value is we use two points to guess. The right side is not satisfied, the left is possible. For each k, we can get a group of height[] with a common prefix length greater than or equal to K. In this group, if the largest sa[], the smallest sa[] difference is greater than or equal to K, that is, there is no overlap, and satisfies the conditions, the current k is feasible. */#include<stdio.h>#include<string.h>#defineMAXN 400002intWA[MAXN],WB[MAXN],WV[MAXN],WS[MAXN];intcmpint*r,intAintBintl) {returnr[a]==r[b]&&r[a+l]==r[b+l];}intMaxintXinty) {returnX>y?x:y;}intMinintXinty) {returnX<y?x:y;}voidDaint*r,int*sa,intNintm) {    inti,j,p,*x=wa,*y=wb,*T;  for(i=0; i<m;i++) ws[i]=0;  for(i=0; i<n;i++) ws[x[i]=r[i]]++;  for(i=1; i<m;i++) ws[i]+=ws[i-1];  for(i=n-1; i>=0; i--) sa[--ws[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]&GT;=J) y[p++]=sa[i]-J;  for(i=0; i<n;i++) wv[i]=X[y[i]];  for(i=0; i<m;i++) ws[i]=0;  for(i=0; i<n;i++) ws[wv[i]]++;  for(i=1; i<m;i++) ws[i]+=ws[i-1];  for(i=n-1; i>=0; i--) sa[--ws[wv[i]]]=Y[i];  for(t=x,x=y,y=t,p=1, x[sa[0]]=0, i=1; i<n;i++) X[sa[i]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +; }    return;}intRANK[MAXN],HEIGHT[MAXN];voidCalheight (int*r,int*sa,intN) {    inti,j,k=0;  for(i=1; i<=n;i++) rank[sa[i]]=i;  for(i=0; i<n;height[rank[i++]]=k) for(k?k--:0, j=sa[rank[i]-1];r[i+k]==r[j+k];k++); return;}intR[MAXN],SA[MAXN];intCheckintXintN) {    inti,j; intMinx,maxx; Minx=10000000; Maxx=-1;  for(i=1; i<n;i++)    {        if(height[i]>=x) {Minx=min (Minx,sa[i]);//Find the smallest locationMaxx=max (Maxx,sa[i]);//find the largest location            if(maxx-minx>=x)return 1; }        Else{Minx=Sa[i]; Maxx=Sa[i]; }    }    return 0;}intMain () {intI,j,n;  while(SCANF ("%d", &n)! =EOF) {        if(!n) Break; intBefore= -;  for(i=0; i<n;i++)        {            intZ; scanf ("%d",&z); R[i]=z-before+ -; Before=Z; } R[n]=0; Da (r,sa,n+1, the+ -);        Calheight (R,sa,n); intans=0; intll,rr,m; ll=1, rr=N;  while(ll<=RR) {m= (LL+RR)/2; if(check (m,n)) {ans=L; ll=m+1; }            ElseRR=m-1; }        if(ans<4) printf ("0\n"); Elseprintf ("%d\n", ans+1); }}

poj1743 suffix array + two-point answer

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.