HDU-1403 Suffix Array Preliminary

Source: Internet
Author: User

Test instructions: Find the longest common substring of two strings
Two strings are concatenated and then a height array is obtained.
Note that the two SA values must be on a different side
The subject is used to test the template, think of Qingdao that overturned feeling very bad

#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;}CharSTR[MAXN];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]    voidGet () {N=strlen (str+1); Rep (I,0, -) cnta[i]=0; Rep (I,1, n) cnta[str[i]]++; Rep (I,1, -) 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;CharSTR1[MAXN];intMain () { while(~S1 (str)) {intLen=strlen (str+1);        S1 (STR1); strcat (str+1, str1+1);        Sa.get (); Sa.ht ();intN=strlen (str+1);intans=0; for(inti =1; I <= N; i++) {if(Ans<sa.height[i]) {if((sa.sa[i]<=len&&sa.sa[i-1]>len) | | (Sa.sa[i]>len&&sa.sa[i-1]<=len)) {ans=sa.height[i];    }}} println (ANS); }return 0;}

HDU-1403 Suffix Array Preliminary

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.