题目大意:给你一些音符每个音符在1-88然后让你寻找最长能多长的连续音符满足下列条件
1长度至少为5
2这段数字同时加上一个数或者减去一个能变成另外一段一样的音符
题解思路:如果能相等那么他们的增加量和减少量也是相等的所以用后缀数字寻找其中的两端LCP大于K并且没有重叠
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int mx = 20005;
int wa[mx],wb[mx],wv[mx],c[mx];
int rk[mx],height[mx],sa[mx];
int a[mx],r[mx];
bool cmp(int *r,int a,int b,int l){
return r[a] == r[b] && r[a+l] == r[b+l];
}
void da(int *r,int *sa,int n,int m){
int *x = wa,*y = wb;
for(int i = 0; i < m; i++) c[i] = 0;
for(int i = 0; i < n; i++) c[x[i] = r[i]]++;
for(int i = 1; i < m; i++) c[i] += c[i-1];
for(int i = n-1; i >= 0; i--) sa[--c[x[i]]] = i;
for(int j = 1,p = 1; p < n;j*=2,m = p){
p = 0;
for(int i = n-j; i < n; i++) y[p++] = i;
for(int i = 0; i < n; i++) if(sa[i]>=j) y[p++] = sa[i]-j;
for(int i = 0; i < m; i++) c[i] = 0;
for(int i = 0; i < n; i++) wv[i] = x[y[i]];
for(int i = 0; i < n; i++) c[wv[i]]++;
for(int i = 1; i < m; i++) c[i]+=c[i-1];
for(int i = n-1; i >= 0; i--) sa[--c[wv[i]]] = y[i];
swap(x,y),p = 1,x[sa[0]] = 0;
for(int i = 1; i < n; i++)
x[sa[i]] = cmp(y,sa[i],sa[i-1],j)?p-1:p++;
}
}
void calcheight(int *r,int n){
int k = 0,j;
for(int i = 1; i <= n; i++) rk[sa[i]] = i;
for(int i = 0; i < n; height[rk[i++]] = k)
for(j = sa[rk[i]-1],k?k--:0; r[j+k] == r[i+k];k++);
}
bool check(int mid,int n){
int L = n+5,R = -1;
for(int i = 1; i <= n; i++){
if(height[i] >= mid){
L = min(L,min(sa[i],sa[i-1]));
R = max(R,max(sa[i],sa[i-1]));
if(R-L>=mid) return true;
}
else{
R = -1,L = n+5;
}
}
return false;
}
int main(){
int n;
while(scanf("%d",&n)&&n){
for(int i = 0; i < n; i++)
scanf("%d",&a[i]);
for(int i = 0; i < n-1; i++)
r[i] = a[i]-a[i+1]+100;
r[--n] = 0;
da(r,sa,n+1,200);
calcheight(r,n);
int L = 0,R = n+1;
while(L < R){
int mid = (L+R+1)>>1;
if(check(mid,n))
L = mid;
else
R = mid-1;
}
printf("%d\n",L<4?0:L+1);
}
return 0;
}