Suffix array two • Repeat Melody 2 time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description
Small hi usually a big hobby is playing the piano. We know that a musical melody is represented as a series of numbers consisting of N in length. Little hi has practiced a lot of music and found that many of the works themselves contain the same melody.
Melody can be expressed as a continuous sequence of numbers, similar melodies in the original sequence is not overlapping, for example, 1 2 3 2 3 2 1 in 2 3 2 appeared once, 2 3 appeared two times, little hi want to know a melody in the frequency of at least two times what is the longest melody?
Tips on how to solve problems
Input
The first line is an integer N. 1≤n≤100000
Next there are N integers that represent the number of each tone. 1≤ Digital ≤1000
Output
An integer line that represents the answer.
-
-
Sample input
-
-
81 2 3 2 3 2 3 1
-
-
Sample output
-
2
Analysis: Suffix array + two points;
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<unordered_map>#include<queue>#include<stack>#include<vector>#include<list>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineVI vector<int>#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)#definePII pair<int,int>#defineLson L, Mid, Ls[rt]#defineRson mid+1, R, Rs[rt]#defineSYS system ("pause")#defineFreopen freopen ("In.txt", "R", stdin)Const intmaxn=1e5+Ten;using namespacestd;ll gcd (ll p,ll q) {returnq==0? P:GCD (q,p%q);} ll Qpow (ll p,ll q) {ll F=1; while(q) {if(q&1) f=f*p;p=p*p;q>>=1;}returnF;} inline ll read () {ll x=0;intf=1;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;}intN,M,K,T,CNTA[MAXN],CNTB[MAXN],SA[MAXN],LEV[MAXN],CH[MAXN],HEIGHT[MAXN],A[MAXN],B[MAXN],TSA[MAXN];voidsolve () { for(inti =0; I <1001; i + +) cnta[i] =0; for(inti =1; I <= N; i + +) Cnta[ch[i]] + +; for(inti =1; I <1001; i + +) Cnta[i] + = cnta[i-1]; for(inti = n; I I--) sa[cnta[ch[i]]---] =i; lev[sa[1]] =1; for(inti =2; I <= N; i + +) {Lev[sa[i]]= Lev[sa[i-1]]; if(Ch[sa[i]]! = ch[sa[i-1]]) Lev[sa[i]] + +; } for(intL =1; Lev[sa[n]] < n; L <<=1) { for(inti =0; I <= N; i + +) cnta[i] =0; for(inti =0; I <= N; i + +) cntb[i] =0; for(inti =1; I <= N; i + +) {Cnta[a[i]= Lev[i]] + +; Cntb[b[i]= (i + L <= N)? Lev[i + L]:0] ++; } for(inti =1; I <= N; i + +) Cntb[i] + = cntb[i-1]; for(inti = n; I I--) tsa[cntb[b[i]]---] =i; for(inti =1; I <= N; i + +) Cnta[i] + = cnta[i-1]; for(inti = n; I I--) sa[cnta[a[tsa[i] []---] =Tsa[i]; lev[sa[1]] =1; for(inti =2; I <= N; i + +) {Lev[sa[i]]= Lev[sa[i-1]]; if(A[sa[i]]! = a[sa[i-1]] || B[sa[i]]! = b[sa[i-1]]) Lev[sa[i]] + +; } } for(inti =1, j =0; I <= N; i + +) { if(j) J--; while(Ch[i + j] = = Ch[sa[lev[i]-1] + j]) J + +; Height[lev[i]]=J; }}BOOLCheckintp) { intMa,mi; for(intI=1; i<=n;i++) { if(height[i]<p) {ma=Sa[i]; Mi=Sa[i]; } Else{ma=Max (ma,sa[i]); Mi=min (mi,sa[i]); if(ma-mi>=p)return true; } } return false;}intMain () {inti,j; scanf ("%d",&N); Rep (I,1, N) ch[i]=read (); Solve (); intL=1, r=n/2, ans=0; while(l<=r) {intMid=l+r>>1; if(Check (mid)) ans=mid,l=mid+1; Elser=mid-1; } printf ("%d\n", ans); //System ("Pause"); return 0;}
Suffix array two • Repeat Melody 2