Main topic
Find a continuous sequence of sub-sequences appear two times, and these two times disjoint, as long as each number in the subsequence is added/minus a number to get a new sequence, can also be considered the same
So that means the title can be converted to find two sub-sequences, the difference between each number in the two subsequence is equal
So we can ask for the difference between all the numbers 22, and then set the suffix array based on this value to solve the problem with a two-point answer.
1#include <cstdio>2#include <cstring>3#include <iostream>4 5 using namespacestd;6 Const intN =20010;7 intRank[n], sa[n], height[n];8 intWa[n], wb[n], tmp[n], wv[n];9 intA[n], b[n];Ten One intcmpint*r,intAintBintl) A { - returnR[A]==R[B] && r[a+l]==r[b+l]; - } the - voidGetsa (int*r,int*sa,intNintm) - { - inti,j,p; + int*x=wa, *Y=WB, *T; - for(i=0; I<m; i++) tmp[i]=0; + for(i=0; I<n; i++) tmp[x[i]=r[i]]++; A for(i=1; I<m; i++) tmp[i]+=tmp[i-1]; at for(i=n-1; i>=0; i--) sa[--tmp[x[i]]]=i; - - //the initial p=1 is to prevent only one element from exiting the loop. - for(p=1, j=1; P<n; j*=2, m=p) { - //sort the Second keyword -p=0; in for(i=n-j; i<n; i++) y[p++]=i; - for(i=0; I<n; i++)if(SA[I]>=J) y[p++]=sa[i]-J; to + for(i=0; I<n; i++) wv[i]=X[y[i]]; - for(i=0; I<m; i++) tmp[i]=0; the for(i=0; I<n; i++) tmp[wv[i]]++; * for(i=1; I<m; i++) tmp[i]+=tmp[i-1]; $ for(i=n-1; i>=0; i--) sa[--tmp[wv[i]]]=Y[i];Panax Notoginsengt=x,x=y,y=T; -x[sa[0]]=0; the for(p=1, i=1; I<n; i++) +X[sa[i]] = CMP (y,sa[i-1],sa[i],j)? p1:p + +; A } the + return; - } $ $ voidCallheight (int*r,int*sa,intN) - { - inti,j,k=0; the for(i=1; I<=n; i++) rank[sa[i]]=i; - Wuyi for(i=0; I<n; height[rank[i++]]=k) the for(k?k--:0, j=sa[rank[i]-1] ; R[I+K]==R[J+K]; k++); - return; Wu } - About BOOLCheckintMintN) $ { - intl=sa[0], r=sa[0]; - for(intI=1; I<n; i++){ - if(height[i]<m) { AL=sa[i], r=Sa[i]; + } the Else{ -L=min (sa[i], L); $R=Max (Sa[i], R); the if(r-l>m)return true; the } the } the return false; - } in the intMain () the { About //freopen ("a.in", "R", stdin); the intN; the while(SCANF ("%d", &N), N) the { + for(intI=0; I<n; i++) scanf ("%d", &a[i]); - for(intI=0; i<n-1; i++) b[i]=a[i+1]-a[i]+ -; theb[n-1]=0;BayiGetsa (B,sa,n, $); theCallheight (b,sa,n-1); the - intL=0, R=n, ans=0; - while(l<=R) { the intM= (l+r) >>1; the if(!check (m,n)) r=m-1; the Else{ theans=m; -l=m+1; the } the } the if(ans>=4) printf ("%d\n", ans+1);94 ElsePuts"0"); the } the return 0; the}
POJ 1743 suffix Array