P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

题目描述

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.

每次只能从两边取,要求取出来之后字典序最小

输入输出格式

输入格式:

 

* 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

 


输出格式:

 

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.

 

输入输出样例

输入样例#1: 复制

6
A
C
D
B
C
B

输出样例#1: 复制

ABCBCD

说明

每输出80个字符需要输出一个换行。

总结:贪心 + 后缀数组

贪心应该很好想

每次在两头选择字典序最小的

如果字典序相同, 暴力就没法搞了,复杂度炸掉

考虑后缀数组比较O(nlogn)处理,O(1)比较

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
int wr[maxn], y[maxn], c[maxn], rk[maxn], sa[maxn];
int p, k, ln, H[maxn], n, m; char ch[maxn];
void SA(int x) {
	m = x;
	for (int i = 1; i <= n; ++i) rk[i] = ch[i];
	for (int i = 1; i <= n; ++i) c[rk[i]]++;
	for (int i = 1; i <= m; ++i) c[i] += c[i - 1];
	for (int i = n; i >= 1; --i) sa[c[rk[i]]--] = i;
	ln = 1; p = 0;
	while(p < n) {
		k = 0;
		for (int i = n - ln + 1; i <= n; ++i) y[++k] = i;
		for (int i = 1; i <= n; ++i) if(sa[i] > ln) y[++k] = sa[i] - ln;
		memset(c, 0, sizeof c);
		for (int i = 1; i <= n; ++i) wr[i] = rk[i];
		for (int i = 1; i <= n; ++i) c[wr[y[i]]]++;
		for (int i = 1; i <= m; ++i) c[i] += c[i - 1];
		for (int i = n; i >= 1; --i) sa[c[wr[y[i]]]--] = y[i];
		for (int i = 1; i <= n; ++i) wr[i] = rk[i];
		rk[sa[1]] = 1; p = 1;
		for (int i = 2; i <= n; ++i) {
			if(!(wr[sa[i]] == wr[sa[i - 1]] && wr[sa[i] + ln] == wr[sa[i - 1] + ln])) ++p;
			rk[sa[i]] = p;
		} m = p; ln <<= 1;
	}
}
int main() {
	int pp;
	scanf("%d", &pp);
	for (int i = 1; i <= pp; ++i) {
		char c; cin >> c;
		ch[i] = c;
	} ch[pp + 1] = 1;
	for (int i = 1; i <= pp; ++i) ch[pp + 1 + i] = ch[pp + 1 - i];
	n = pp + pp + 1; SA(300);
	int l = 1, r = pp + 2;
	for (int i = 1; i <= n; ++i) rk[sa[i]] = i;
	for (int d = 1; d <= pp; ++d) {
		if(rk[l] < rk[r]) printf("%c", ch[l++]);
		else printf("%c", ch[r++]);
		if(d % 80 == 0) printf("\n");
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/oi-forever/p/9174428.html

时间: 2024-10-09 19:03:30

P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold的相关文章

P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 解题报告

P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 题意 给一个字符串,每次可以从两边中的一边取一个字符,要求取出的字符串字典序最小 可以Hash+二分 也可以SA 首先贪心选字典序小的 然后遇到相等的了比Rank数组,把原串倍长一下就可以比了. Code: #include <cstdio> #include <algorithm> const int N=6e4+10; char s[N],ans[N]; int sa[N],Rank[N]

贪心 洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

[USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 题目描述 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 organ

[Luogu2870] [USACO07DEC]最佳牛线Best Cow Line(贪心+后缀数组)

[Luogu2870] [USACO07DEC]最佳牛线Best Cow Line(贪心+后缀数组) 题面 FJ打算带他的\(N(1 \leq N \leq 30,000)\)头奶牛去参加一年一度的"全美农场主大奖赛".在这场比赛中,每个参赛者都必须让他的奶牛排成一列,然后领她们从裁判席前依次走过. 今年,竞赛委员会在接受队伍报名时,采用了一种新的登记规则:他们把所有队伍中奶牛名字的首字母取出,按它们对应奶牛在队伍中的次序排成一列(比如说,如果FJ带去的奶牛依次为Bessie.Sylv

[luoguP2870] [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(后缀数组)

传送门 数据小的话贪心就行. 可以把这个串翻转再接到后面,再求后缀数组,求出 rank 数组就很简单了. ——代码 1 #include <cstdio> 2 #include <iostream> 3 #define N 60001 4 5 int n, len, m = 256, sum; 6 int buc[N], x[N], y[N], sa[N], rank[N]; 7 char s[N]; 8 9 inline void build_sa() 10 { 11 int i

POJ 3623 Best Cow Line, Gold(模拟)

题意  给你一个字符序列   你每次可以从它的头部或尾部拿出一个字符组成一个新的字符序列   输出这样做能达到的最小的字符序列   每行最多输出80个字符(开始被这个坑了好久) 直接模拟就行  哪边小就选哪边  相等就往内看 #include<cstdio> #include<iostream> #include<string> using namespace std; const int N = 30010; int main() { char s[N][2]; in

HDU 3623 Best Cow Line, Gold(模拟,注意思路,简单)

题目 POJ 3617 和 这道题题目一样,只是范围稍稍再小一点. //模拟试试 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; char s[30010][2]; bool bijiao(int st,int ed) { if(st==ed) return true; if(s[st][0]<s[ed][0]) return true; else if(s

POJ 3623 Best Cow Line, Gold(字符串处理)

题意:给你一个字符串,让你重新排列,只能从头或者尾部取出一个放到新字符串队列的最后.按照字典序. 解决方法:比较前后两个的大小,谁小输出谁,相等,就往当中比来确定当前应该拿最前面的还是最后面的,如果再相等就继续.... 所以比较这个动作的单一功能,可以写成一个check函数,方便操作也方便递归. #include<iostream> #include<cstring> using namespace std; #define MAX 30005 char str[MAX]; int

POJ 题目3623 Best Cow Line, Gold(后缀数组rank简单应用)

Best Cow Line, Gold Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5063   Accepted: 1806 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 arrang

poj3623 Best Cow Line, Gold(贪心)

题目链接: huangjing 思路: 选取字典序最小的串,那么值得考虑的是当两端出现相等时,继续比较,直到出现不同的结果.. 题目: Best Cow Line, Gold Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4574   Accepted: 1682 Description FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farme