The question is easy to understand, but modeling requires the attention:
The substring formed by a substring constant is considered to be the same as the substring, and the longest non-overlapping duplicate substring is obtained.
Description in question
- Is disjoint from (I. e., non-overlapping with) at least one of its other appearance (s)
This means that they cannot overlap. For example
1, 52, 54
1, 2, 3, 52, and 54 satisfy the question. The difference is 51.
The enumerated difference is definitely not good ------ I understood the problem ::
After subtracting the previous one, we get:,. Note that the answer is 2 + 1,
If this method is used
1, 2, 3, 52, 53,54, 55
After subtracting the previous one, we get:, and 1. The answer is 3 + 1. It must be wrong!
In order to exclude interference from the first item, delete the first item or add the number greater than 88 to the next item, or delete the first item directly after making a difference.
Thought learned in this question ----- incremental elimination of adjacent items
#include <cstdio>#include <iostream>#include <string>#include <algorithm>#include <cmath>using namespace std;const int MAXN = 20200;int rk[MAXN],sa[MAXN],s[MAXN],tmp[MAXN],lcp[MAXN],n,k;bool cmpSa(int i,int j){ if(rk[i] != rk[j])return rk[i]<rk[j]; else { int ri = i+k<=n?rk[i+k]:-1; int rj = j+k<=n?rk[j+k]:-1; return ri<rj; }}void consa(){ for(int i=0;i<=n;i++) sa[i]=i,rk[i]=i<n?s[i]:-1; for(k=1;k<=n;k*=2) { sort(sa,sa+n+1,cmpSa); tmp[sa[0]]=0; for(int i=1;i<=n;i++) { tmp[sa[i]]=tmp[sa[i-1]]+(cmpSa(sa[i-1],sa[i])?1:0); } for(int i=0;i<=n;i++) rk[i]=tmp[i]; }}void construct_lcp(){ //n=strlen(s); for(int i=0; i<=n; i++)rk[sa[i]]=i; int h=0; lcp[0]=0; for(int i=0;i<n;i++) { int j=sa[rk[i]-1]; if(h>0)h--; for(; j+h<n && i+h<n; h++) { if(s[j+h]!=s[i+h])break; } lcp[rk[i]-1]=h; }}int C(int x){ int ret=0,last=0,mmin=n,mmax=0; for(int i=0;i<=n;i++) { if(lcp[i]>=x) { ret++; mmin=min(sa[i],min(mmin,sa[i+1])); mmax=max(sa[i],max(mmax,sa[i+1])); } else { if(i>=1 && (mmax-mmin) >=x)return 1;////////////////// ret=mmax=0; mmin=n; } } return 0;}int main(){ //freopen("poj 1743.txt","r",stdin); int t; while(scanf("%d",&n)!=EOF && n) { for(int i=0;i<n;i++) scanf("%d",&s[i]); /*for(int i=1;i<n;i++) s[i]=s[i]-s[i-1];*/ for(int i=n-1;i>=1;i--) s[i]=s[i]-s[i-1]+100; s[n]=-200; //s[0]=-111900; consa(); construct_lcp(); ////////////////////// //for(int i=0;i<=n;i++) // printf("sa[i]=%d lcp[%d]=%d\n", sa[i],i,lcp[i]); ///////////////////// int d=0,up=n+1,mid; while(up>d+1) { mid=(d+up)/2; if(C(mid))d=mid; else up=mid; } if(d>=4) printf("%d\n",d+1); else printf("0\n"); } return 0;}