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

这个代码应该是跪的吧,但是过了

Posted by MrBird at 2017-03-01 17:30:11 on Problem 1743
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=20500;
int s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
int Rank[maxn], height[maxn];
void build_sa(int m, int n){
	int i, *x=t, *y=t2;
	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(int k=1;k<=n;k<<=1){
		int 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[y[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);
		p=1;x[sa[0]]=0;
		for(i=1;i<n;i++)
			x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
		if(p>=n)break;
		m=p;
	}
}
void geth(int n){
	int i, j, k=0;
	for(i=0;i<n;i++)Rank[sa[i]]=i;
	for(i=0;i<n;i++){
		if(k)k--;
		if(Rank[i]==0)break;
		j=sa[Rank[i]-1];
		while(s[i+k]==s[j+k])k++;
		height[Rank[i]]=k;
	}
}
bool check(int mid, int n){
	int i, j;
	for(i=1;i<n;i++){
		if(height[i]<mid)continue;
		for(j=i-1;j>=1;j--){
			if(abs(sa[i]-sa[j])>mid)return true;
			if(height[j]<mid)break;
		}
	}
	return false;
}
int main()
{
	while(scanf("%d", &n), n){
		for(int i=0;i<n;i++)scanf("%d", &s[i]);
		for(int i=0;i<n-1;i++)s[i]=s[i+1]-s[i]+100;
		s[n-1]=0;
		build_sa(200, n);
		geth(n);
		int l=3, r=n-1, ans=0;
		while(l<=r){
			if(r-l<=1){
				if(check(r, n))ans=r;
				else ans=l;
				break;
			}
			int mid=(l+r)/2;
			if(check(mid, n))l=mid;
			else r=mid;
		}
		if(ans<4)printf("0\n");
		else printf("%d\n", ans+1);
	}
}

check的部分参照网上某blog的ac代码,过了。但其实check部分是n^2的,大概可能也许 是不行的?
PS:后来重写了一份

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