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

为什么RE?我将数组同时加上一个数,然后插入到后面,再求后缀数组,结果为什么是PE

Posted by lhshaoren at 2012-08-19 16:19:21 on Problem 1743 and last updated at 2012-08-20 10:21:32
#include <iostream>
using namespace std;
const int nMax = 40100;
const int mMax = 200;
int sa[nMax], rank[nMax], height[nMax];
int wx[nMax], wy[nMax];
int _ws[mMax], wv[nMax];
int N;
int A[nMax];

int cmp(int *t, int a, int b, int l)
{
	if(t[a] == t[b] && t[a + l] == t[b + l]) return 1;
	return 0;
}

void da(int *t, int n, int m)
{
	int *x = wx, *y = wy, *temp;
	int i, j, p;
	for(i = 0; i < m; ++ i) _ws[i] = 0;
	for(i = 0; i < n; ++ i) _ws[x[i] = t[i]] ++;
	for(i = 1; i < m; ++ i) _ws[i] += _ws[i - 1];
	for(i = n - 1; i >= 0; -- i) sa[-- _ws[x[i]]] = i;

	for(j = 1, p = 1; p != n; j = 2 * j, m = p)//p
	{
		for(i = n - j, p = 0; i < n; ++ i) y[p ++] = i;
		for(i = 0; i < n; ++ i)//i
			if(sa[i] >= j)
				y[p ++] = sa[i] - j;

		for(i = 0; i < m; ++ i) _ws[i] = 0;
		for(i = 0; i < n; ++ i) wv[i] = x[y[i]];
		for(i = 0; i < n; ++ i) _ws[wv[i]] ++;
		for(i = 1; i < m; ++ i) _ws[i] += _ws[i - 1];
		for(i = n - 1; i >= 0; -- i) sa[-- _ws[wv[i]]] = y[i];
		
		for(temp = x, x = y, y = temp, i = 1, p = 1, x[sa[0]] = 0;i < n;i ++)
			x[sa[i]] = cmp(y, sa[i], sa[i - 1], j) ? p - 1 : p ++;
	}
}

void calHeight(int n)
{
	int i, j, k = 0;//k
	for(i = 1; i <= n; ++ i) rank[sa[i]] = i;
	for(i = 0; i < n; height[rank[i]] = k, i ++)
		for(k ? k -- : 0, j = sa[rank[i] - 1]; A[j + k] == A[i + k]; k ++);//
}

int bsearch(int n)
{
	int l, r;
	l = 0; r = N;
	while(l + 1 < r)
	{
		int flag = 0;
		int i;
		int max = 0, min = nMax;
		int mid = (l + r) / 2;
		for(i = 2; i <= n; ++ i)//height[1]始终为0
		{
			if(height[i] < mid)
			{
				if(max - min >= mid)
				{
					flag = 1;
					break;
				}
				max = 0;
				min = nMax;
			}
			else
			{
				int a = sa[i],
					b = sa[i - 1];
				if(a > N) a -= 1 + N;
				if(b > N) b -= 1 + N;
				if(a > max) max = a;
				if(b > max) max = b;
				if(a < min) min = a;
				if(b < min) min = b;
			}
		}
		if(max - min >= mid)
			flag = 1;
		if(flag) l = mid;
		else r = mid;
	}
	return l;
}

int main()
{
	//freopen("e://data.in", "r", stdin);
	while(scanf("%d", &N) != EOF && N)
	{
		int i, k;
		int ans = 0;
		for(i = 0; i < N; ++ i)
			scanf("%d", &A[i]);
		A[N] = 180;
		for(k = 1; k < 88; k ++)
		{
			for(i = 0; i < N; ++ i)
				A[N + 1 + i] = A[i] + k;
			A[2 * N + 1] = 0;
			da(A, 2 * N + 2, 182);
			calHeight(2 * N + 1);
			int res = bsearch(2 * N + 1);
			if(res > ans)
				ans = res;
		}
		if(ans >= 5)
			printf("%d\n", ans);
		else
			printf("0\n");
	}
	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