UVA-10298 suffix Array (viewing only)

Source: Internet
Author: User

Test instructions: Finding the minimum cycle section
\ (kmp\) can be 20ms through, while \ (da\) implements a suffix array that cannot be passed within 3000ms
Heard to use \ (dc3\) only reluctantly card, here only \ (da\) implementation

#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];intNstructsa{intRANK[MAXN],SA[MAXN],TSA[MAXN],A[MAXN],B[MAXN];intCNTA[MAXN],CNTB[MAXN];intheight[maxn],best[maxn][ A],n;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-1],best[j+ (1<< (i-1))][i-1]); }        }    }intQueryintLintR) {if(L==R)return-oo;if(l>r) swap (L,R); l++;intK=log (1.0*r-l+1)/log (2.0);returnMin (best[l][k],best[r-(1&LT;&LT;K)+1][k]); }}sa;intMain () { while(~S1 (str)) {if(str[1]=='. '&&str[2]==0) Break; N=strlen (str+1);        Sa.get (n);        Sa.ht (); SA.RMQ ();intans=1; Rep (I,1N-1){if(n%i!=0)Continue;//if (sa.sa[i]==1) break;//if (SA.SA[I]==SA. RANK[1]) break;//Cout<<sa.query (I,SA. RANK[1]) <<endl;//if (Sa.query (I,sa. RANK[1]) ==n-sa.sa[i]+1) {//Ans=max (ans,n/(n-sa.sa[i));//          }            if(Sa.query (SA. rank[1],sa. rank[1+i]) (==n-i) {ans=n/i; Break;    }} println (ANS); }return 0;}

UVA-10298 suffix Array (viewing only)

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.