Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

RT

Posted by 2730052770 at 2017-12-04 19:06:31 on Problem 1743
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define N 20005
using namespace std;
int s[N],sa[N],rk[N],ht[N],c[N],a[2][N],mx[20][N],mn[20][N],lg[N];
void build(int n,int m)
{
	int i,k,p,*x=a[0],*y=a[1];
	for(i=0;i<m;i++)c[i]=0;
	for(i=0;i<n;i++)c[x[i]=s[i]]++;
	for(i=1;i<m;i++)c[i]+=c[i-1];
	for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
	for(k=1;k<=n;k<<=1){
		p=0;
		for(i=n-k;i<n;i++)y[p++]=i;
		for(i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
		for(i=0;i<m;i++)c[i]=0;
		for(i=0;i<n;i++)c[x[i]]++;
		for(i=1;i<m;i++)c[i]+=c[i-1];
		for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
		swap(x,y);
		m=1,x[sa[0]]=0,y[n]=-1;
		for(i=1;i<n;i++)
			x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?m-1:m++;
		if(m>=n)break;
	}
	memcpy(rk,x,sizeof(rk));
	k=0,ht[0]=0;
	for(i=0;i<n;i++){
		if(k)k--;
		if(!rk[i])continue;
		int j=sa[rk[i]-1];
		while(s[i+k]==s[j+k]&&i+k<n&&j+k<n)k++;
		ht[rk[i]]=k;
	}
}
void ST(int n){
	memcpy(mx[0],sa,sizeof(sa));
	memcpy(mn[0],sa,sizeof(sa));
	for(int i=1;i<20;i++)
		for(int j=0;j+(1<<i)-1<n;j++)
			mx[i][j]=max(mx[i-1][j],mx[i-1][j+(1<<(i-1))]),
			mn[i][j]=min(mn[i-1][j],mn[i-1][j+(1<<(i-1))]);
	for(int i=2;i<N;i++)lg[i]=lg[i>>1]+1;
}
bool query(int l,int r,int lmt){
	int Lg=lg[r-l+1],len=1<<Lg;
	return max(mx[Lg][l],mx[Lg][r-len+1])-min(mn[Lg][l],mn[Lg][r-len+1])>=lmt;
}
bool check(int mid,int n){
	int pre=0;
	ht[n]=0x80000000;
	for(int i=1;i<=n;i++)if(ht[i]<mid){
		if(query(pre,i-1,mid+1))return 1;//×¢ÒâÊÇmid+1ÒòΪҪÇó²»Öظ´ 
		pre=i;//Ôò²îÖµÊý×é(¼´sÊý×é)ÖеÄÁ½¶ÎÓ¦¸ÃÊÇÖÁÉÙ¸ôÁËÒ»¸öÊýµÄ 
	}//¶ø²»ÄܽôÌù×Å,½ôÌù×ŵĻ°Ô­Êý×é¾Í»áÓÐÖظ´ 
	return 0;
}
int main()
{
	int n;
	while(cin>>n&&n)
	{
		for(int i=0;i<n;i++)scanf("%d",&s[i]);
		for(int i=n-1;i>=1;i--)s[i]-=s[i-1];
		n--;
		for(int i=0;i<n;i++)s[i]=s[i+1]+100;
		s[n]=0;
		build(n,200);
		//for(int i=0;i<n;i++)printf("%d %d %d\n",s[i],sa[rk[i]],ht[i]);
		ST(n);
		int l=4,r=n,mid,ans=-1;
		while(l<=r){
			mid=(l+r)>>1;
			if(check(mid,n)) ans=mid,l=mid+1;
			else r=mid-1;
		}
		cout<<ans+1<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator