HDU 5442 Favorite Donut(后缀数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5442

 

题意:给出一个字符串环,要求以某点作为起点选择顺逆方向吃完整个环,使得字典序最大,如果有多种选择,先选择位置最靠前的再选取顺时针方向

 

思路:先按正方向将串扩成两倍,再按反方向将串扩成两倍,中间用较小的特殊符连接,然后找到后缀最大的字符串,然后利用高度数组逆着判断当前字符串是否仍满足字典序最大,注意判断该后缀所对应的位置是否满足要求

 

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <functional>
#include <deque>
#include <ctime>
#include <string>

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;

typedef long long ll;

const int MAXN = 401000;

int sa[MAXN], height[MAXN], ran[MAXN];
int r[MAXN];


int t1[MAXN], t2[MAXN], c[MAXN];

bool cmp(int *r, int a, int b, int l)
{
	return r[a] == r[b] && r[a + l] == r[b + l];
}

void da(int str[], int sa[], int ran[], int height[], int n, int m)
{
	n++;
	int i, j, p, *x = t1, *y = t2;
	for (i = 0; i < m; i++)c[i] = 0;
	for (i = 0; i < n; i++)c[x[i] = str[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 (j = 1; j <= n; j <<= 1)
	{
		p = 0;
		for (i = n - j; i < n; i++)y[p++] = i;
		for (i = 0; i < n; i++)if (sa[i] >= j)y[p++] = sa[i] - j;
		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]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
		if (p >= n)break;
		m = p;
	}
	int k = 0;
	n--;
	for (i = 0; i <= n; i++)ran[sa[i]] = i;
	for (i = 0; i < n; i++)
	{
		if (k)k--;
		j = sa[ran[i] - 1];
		while (str[i + k] == str[j + k])k++;
		height[ran[i]] = k;
	}
}

char s[MAXN];

struct node
{
	int p, c;

	bool operator < (const node &rhs) const
	{
		if (p == rhs.p) return c < rhs.c;
		return p < rhs.p;
	}
} ans[MAXN];

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int n;
		scanf("%d%s", &n, s);

		int len = n * 4 + 1;
		for (int i = 0; i < n; i++)
			r[i] = r[i + n] = s[i] - 'a' + 2;
		r[n * 2] = 1;

		for (int i = 1; i <= n; i++)
			r[n * 2 + i] = r[n * 3 + i] = s[n - i] - 'a' + 2;
		r[len] = 0;

		da(r, sa, ran, height, len, 30);

		// for (int i = 0; i <= len; i++)
		// 	printf("%d ", sa[i]);

		int num = 0;
		ans[num++].p = sa[len];
		for (int i = len - 1; i > 1; i--)
		{
			//	printf("************%d %d\n", sa[i], sa[i - 1]);
			if (height[i + 1] < n) break;
			if (sa[i] >= n && sa[i] <= n * 2) continue;
			if (sa[i] >= n * 3 + 1) continue;
			ans[num++].p = sa[i];
		}

		for (int i = 0; i < num; i++)
		{
			if (ans[i].p <= n * 2)
			{
				ans[i].p++;
				ans[i].c = 0;
			}
			else
			{
				ans[i].p = n - (ans[i].p - n * 2 - 1);
				ans[i].c = 1;
			}
		}

		sort(ans, ans + num);
		cout << ans[0].p << " " << ans[0].c << endl;
	}
	return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值