POJ-1743 suffix Array Height group

Source: Internet
Author: User

Test instructions: Find the longest non-overlapping length of the same difference substring

This problem has been dragged for several months, now spent a little time should understand a lot, try to analyze

Let's first resolve a degenerate version, and find the longest non-overlapping substring (the difference is 0)
Like what\ (aabaabaa\), then the substring being asked has\ (aab,aba,baa\)Three
How to ask? Enumerate the. Enumeration if there is a length of\ (k\)The longest non-overlapping string of the same substring
However, the suffix array does not directly represent the substring, and can only be indirectly represented by a suffix.
Length is\ (k\)The same substring\ (=>\)The maximum common prefix length is\ (k\)The substring\ (=>\)Maximum public prefix length greater than or equal to\ (k\)Suffix (note non-charging)
And all we ask for is a\ (lcp\), we should group each suffix within each group.\ (lcp\)are greater than or equal to\ (k\)By\ (lcp\)Define can be converted into group by\ (sa\)Sort\ (height\)Must not be less than\ (k\)
According to this rule, let's see how the above example is grouped, which is an enumeration\ (k=3\)When the situation
Index=8 height[1]=0\ (a\)
-----
Index=7 height[2]=1\ (aa\)
-----
Index=4 height[3]=2\ (aabaa\)
Index=1 height[4]=5\ (aabaabaa\)
-----
Index=5 height[5]=1\ (abaa\)
index=2 height[6]=4\ (abaabaa\)
-----
Index=6 height[7]=0\ (baa\)
Index=3 height[8]=3\ (baabaa\)
-----
We have noticed that in this sort of way it is not known whether the two substrings overlap, so we need to record\ (index\)
which\ (index\)Refers to the position of the first character of the suffix in the string (1-headed), which is\ (sa[i]\)
Then we have a way to do it now, because every group is guaranteed.\ (lcp>=k\), that is, if overlapping is counted, each group is a legal grouping (the number of suffixes in the main group is greater than 1)
Now because only the non-overlapping is the legal condition, so we need to remove the illegal suffix, since there is\ (sa\)Value that's good to do if any of the two suffixes within the group\ (index\)For\ (i,j\), then you need\ (i_{max}-j_{min}>=k\)That indicates that the group has at least one pair of eligible
At this point, the enumeration\ (k=3\)The case is true and the resulting groupings are in order exactly\ (aab,aba,baa\)Three, and so on.

Now let's look at the original question: find the non-overlapping substring of the longest same difference and output that length
Since we want the same difference, we convert the original string \ (n\) character (value) to a differential array of \ (n-1\) values
The same enumeration of the longest non-overlapping substring of the same value, if the largest (k\) is set, the answer to restore back is \ (k+1\)
However, it should be noted that for \ (index\), should be satisfied \ (i_{max}-j_{min}>k\), because the difference score group must be set aside "gap", otherwise encountered a critical situation is two non-overlapping substring is connected, And this is a bad molecular string, restore back is the middle of the value is exactly two original string repeated selection, which is the reason for slight changes in conditions
However, the data of this problem should be randomly generated, even if not changed will be AC
Lou, the Lord's problem is really good.

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <string>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#define REP (i,j,k) for (register int i=j;i<=k;i++)#define RREP (i,j,k) for (register int i=j;i>=k;i--)#define EREP (i,u) for (register int i=head[u];~i;i=nxt[i])#define IIN (a) scanf ("%d", &a)#define Lin (a) scanf ("%lld", &a)#define DIN (a) scanf ("%lf", &a)#define S0 (a) scanf ("%s", a)#define S1 (a) scanf ("%s", a+1)#define PRINT (a) printf ("%lld", (ll) a)#define ENTER Putchar ('\n ')#define Blank Putchar (")#define PRINTLN (a) printf ("%lld\n ", (ll) a)#define IOS ios::sync_with_stdio (0)using namespaceStdConst intMAXN =1e6+11;Const intOO =0x3f3f3f3f;Const DoubleEPS =1e-7;typedef Long LongLl;ll Read () {ll x=0, f=1;Register CharCh=getchar (); while(ch<' 0 '|| Ch>' 9 '){if(ch=='-') f=-1; Ch=getchar ();} while(ch>=' 0 '&&ch<=' 9 ') {x=x*Ten+ch-' 0 '; Ch=getchar ();}returnX*f;}intStr[maxn],n;structsa{intRANK[MAXN],SA[MAXN],TSA[MAXN],A[MAXN],B[MAXN];intCNTA[MAXN],CNTB[MAXN];intheight[maxn],best[maxn][ -],n;//height[i]: CP of Sa[i] and Sa[i-1]    voidGetintnn) {n=nn; Rep (I,0,666) cnta[i]=0; Rep (I,1, n) cnta[str[i]]++; Rep (I,1,666) cnta[i]+=cnta[i-1]; Rrep (I,n,1) Sa[cnta[str[i]]--]=i; rank[sa[1]]=1; Rep (I,2, N) {if(Str[sa[i]]==str[sa[i-1]]) {Rank[sa[i]]=rank[sa[i-1]]; }Else{rank[sa[i]]=1+rank[sa[i-1]]; }        } for(intL=1; rank[sa[n]]<n;l<<=1) {Rep (I,1, N) cnta[i]=cntb[i]=0; Rep (I,1, n) cnta[a[i]=rank[i]]++; Rep (I,1, N) cntb[b[i]= (i+l<=n? RANK[I+L]:0)]++; Rep (I,1, N) cnta[i]+=cnta[i-1],cntb[i]+=cntb[i-1]; Rrep (I,n,1) Tsa[cntb[b[i]]--]=i; Rrep (I,n,1) Sa[cnta[a[tsa[i]]]--]=tsa[i]; rank[sa[1]]=1; Rep (I,2, N) {BOOLFlag=a[sa[i]]==a[sa[i-1]]&&b[sa[i]]==b[sa[i-1]];                Flag=!flag; Rank[sa[i]]=rank[sa[i-1]]+flag; }        }    }voidHT () {intj=0; Rep (I,1, N) {if(j) j--; while(Str[i+j]==str[sa[rank[i]-1]+J]) J + +;        Height[rank[i]]=j; }    }voidRMQ () {Rep (I,1, N) best[i][0]=height[i]; for(intI=1;(1<<i) <=n;i++) { for(intj=1; j+ (1<<i)-1<=n;j++) {best[j][i]=min (best[j][i],best[j+ (1<< (i-1))][i-1]); }        }    }intQueryintLintR) {if(L==R)return-oo;if(l>r) swap (L,R); l++;intK=LOG2 (r-l+1);returnMin (best[l][k],best[r-(1&LT;&LT;K)+1][k]); }}sa;BOOLCheckintK) {intMx=-oo,mn=oo; for(intI=2; i<=n;i++) {if(sa.height[i]>=k) {Mx=max (Mx,max (sa.sa[i],sa.sa[i-1])); Mn=min (Mn,min (sa.sa[i],sa.sa[i-1]));if(mx-mn>k)return 1; }Else{Mx=-oo;mn=oo; }    }return 0;}intMain () { while(SCANF ("%d", &n), N) {Rep (I,1, n) str[i]=read ();if(n<5) {printf ("0\ n");Continue; } Rep (I,1, N) str[i]=str[i+1]-str[i]+100; str[n]=0; n--;        Sa.get (n); Sa.ht ();intL=0, r=n,mid,ans=0; while(L&LT;=R) {mid= (l+r) >>1;if(Check (mid)) L=mid+1, Ans=mid;ElseR=mid-1; }if(ans+1>=5) println (ans+1));Elseprintln0); }return 0;}

POJ-1743 suffix Array Height group

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.