POJ 3623 Best Cow Line, Gold

原创 2016年08月28日 16:32:09

Description

FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.

The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows' names.

FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.

FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration in this new order.

Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.

Input

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in the ith position in the original line

Output

The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line.

Sample Input

6
A
C
D
B
C
B

Sample Output

ABCBCD


给出n个字母,每次只能从最前或最后取字母,问最后字典序最小是多少。

把原串反转加到后面去,求一遍后缀数组,然后模拟前后取,选小的即可。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<string,string>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e8;
const int N = 5e5 + 10;
const int read()
{
	char ch = getchar();
	while (ch<'0' || ch>'9') ch = getchar();
	int x = ch - '0';
	while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
	return x;
}

struct Sa
{
	char s[N];
	int rk[2][N], sa[N], h[N], w[N], now, n;
	int rmq[N][20], lg[N];

	bool GetS()
	{
		n = read();
		rep(i, 1, n) scanf("%s", s + i);
		s[n + 1] = 1;
		rep(i, 1, n) s[n + n + 2 - i] = s[i];
		n = n + n + 1;
		return true;
	}

	void getsa(int z, int &m)
	{
		int x = now, y = now ^= 1;
		rep(i, 1, z) rk[y][i] = n - i + 1;
		for (int i = 1, j = z; i <= n; i++)
			if (sa[i] > z) rk[y][++j] = sa[i] - z;

		rep(i, 1, m) w[i] = 0;
		rep(i, 1, n) w[rk[x][rk[y][i]]]++;
		rep(i, 1, m) w[i] += w[i - 1];
		per(i, n, 1) sa[w[rk[x][rk[y][i]]]--] = rk[y][i];
		for (int i = m = 1; i <= n; i++)
		{
			int *a = rk[x] + sa[i], *b = rk[x] + sa[i - 1];
			rk[y][sa[i]] = *a == *b&&*(a + z) == *(b + z) ? m - 1 : m++;
		}
	}

	void getsa(int m)
	{
		//n = strlen(s + 1);
		rk[1][0] = now = sa[0] = s[0] = 0;
		rep(i, 1, m) w[i] = 0;
		rep(i, 1, n) w[s[i]]++;
		rep(i, 1, m) rk[1][i] = rk[1][i - 1] + (bool)w[i];
		rep(i, 1, m) w[i] += w[i - 1];
		rep(i, 1, n) rk[0][i] = rk[1][s[i]];
		rep(i, 1, n) sa[w[s[i]]--] = i;

		rk[1][n + 1] = rk[0][n + 1] = 0;	//多组的时候容易出bug
		for (int x = 1, y = rk[1][m]; x <= n && y <= n; x <<= 1) getsa(x, y);
		for (int i = 1, j = 0; i <= n; h[rk[now][i++]] = j ? j-- : j)
		{
			if (rk[now][i] == 1) continue;
			int k = n - max(sa[rk[now][i] - 1], i);
			while (j <= k && s[sa[rk[now][i] - 1] + j] == s[i + j]) ++j;
		}
	}

	void getrmq()
	{
		h[n + 1] = h[1] = lg[1] = 0;
		rep(i, 2, n) rmq[i][0] = h[i], lg[i] = lg[i >> 1] + 1;
		for (int i = 1; (1 << i) <= n; i++)
		{
			rep(j, 2, n)
			{
				if (j + (1 << i) > n + 1) break;
				rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << i - 1)][i - 1]);
			}
		}
	}

	int lcp(int x, int y)
	{
		int l = min(rk[now][x], rk[now][y]) + 1, r = max(rk[now][x], rk[now][y]);
		return min(rmq[l][lg[r - l + 1]], rmq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
	}

	void work()
	{
		GetS();	getsa(256);
		int tot = 0;
		for (int l = 1, r = n / 2; l <= r;)
		{
			if (rk[now][l] < rk[now][n + 1 - r]) printf("%c", s[l++]);
			else printf("%c", s[r--]);
			if (++tot % 80 == 0) putchar(10);
		}
		putchar(10);
	}
}sa;

int main()
{
	sa.work();
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 3623 Best Cow Line, Gold (题意和3617一样只要代码中的范围改一下)

Best Cow Line, Gold Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 426...
  • YJX_xx
  • YJX_xx
  • 2014年06月26日 10:26
  • 687

Cpp环境【POJ3623】【Usaco2007Dec】【Vijos2993】Best Cow Line, Gold 最佳队列[2]

POJ3623 Best Cow Line,Gold 原题Description 问题描述 FJ is about to take his N (1 ≤ N ≤ 30,000) cows to th...

POJ 3623 Best Cow Line, Gold(模拟)

题意  给你一个字符序列   你每次可以从它的头部或尾部拿出一个字符组成一个新的字符序列   输出这样做能达到的最小的字符序列   每行最多输出80个字符(开始被这个坑了好久) 直接模拟就行  哪边...

POJ 3623 Best Cow Line

Best Cow Line, Gold Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 402...

【bzoj 1692】【bzoj 1640】【poj 3617】【poj 3623】Best Cow Line 好牛队 双倍经验水水水

这破题虐了我一晚上…… 读完题就正解了…… 一晚上没调出来…… 猜猜怎么回事…… 我现在仍然在被DQS学长嘲笑…… QAQ…… DQS学长花了五分钟打出来了…… 打出来了…… 我我我我...

POJ--3617 Best Cow Line

Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15789   ...

POJ 3617Best Cow Line

Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" comp...

POJ 3617 Best Cow Line 贪心算法

题目链接:Best Cow Line 贪心策略是:一直从S字串两侧寻找小的字符,特殊情况是:如果两侧字符一样,向内侧比较并获取答案。(不过感觉这步做了很多无用功,尤其是回文串的情况) 代码如下: ...
  • Kiritow
  • Kiritow
  • 2016年04月14日 21:34
  • 202

POJ 3617 Best Cow Line(字典序最小问题)

Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" comp...

POJ 3617 Best Cow Line (贪心)

Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11618   ...
  • lwt36
  • lwt36
  • 2015年02月21日 21:54
  • 310
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 3623 Best Cow Line, Gold
举报原因:
原因补充:

(最多只允许输入30个字)