3429: Best Cow Line
Total Submit: 197 Accepted:29
Description
FJ is about to take his N (1 <= N <= 3,0000) 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
Hint
Step Original New
#1 ACDBCB
#2 CDBCB A
#3 CDBC AB
#4 CDB ABC
#5 CD ABCB
#6 D ABCBC
#7 ABCBCD
Source
分析:在POJ上N最大为2000,不知为何在这里是30000!!!显然最朴素的O(n^2)算法是不能用了。下面我说一下我的线性算法。
考察字符串str[s..t],如果str[s] != str[t],当然是取小的。当str[s] == str[t]时,从两边向中间靠,直到对应位置字符不相等或某个字符不小于str[s]。
//假设str[s..s1]和str[t1..t]分别对应相等(即对称)且每个字符都小于str[s]
if (str[s1 + 1] < str[t1 - 1]) {
//先输出str[s],由假设,str[s + 1] < str[s] = str[t],故接着输出str[s + 1],其余同理
输出str[s..s1]
}
else if (str[s1 + 1] > str[t1 - 1]) {
//code here
}
注意这里用了str[s..s1]中每个字符都小于str[s]的假设,这样一来就可以直接输出str[s..s1](或str[t1..t])这一整段而不是一个字符。复杂之处就在于不满足这个条件的情况。
if (str[s1 + 1] > str[s]) {
//str[s1 + 1]一定是在str[s..s1]和str[t1..t]都输出之后才输出
//不论str[t1 - 1]是什么,先输出str[t1..t]一定是可以的
输出str[t1..t]
}
else if (str[t1 - 1] > str[s]) {
//code here
}
else { //str[s1 + 1] == str[t1 - 1] == str[s]
}
为什么str[s1 + 1] == str[s]的情况要单独讨论呢?考虑str = " CBACBBAABBC ABC",虽然"CBACBBA"这一段是对称的,但是这里应该先输出两端的"CBA“,因为输出左边的"CBA"之后,左边的"CBB"不仅要和右边的"CBB"比较大小,还要和右边还未输出的"CBA"比较。但是反过来,如果str = "CBBCBADDABCBBC",就可以直接输出"CBBCBA",因为左边第二个C开头的一段满足"CBA" < "CBB"。把str按s1 + 1 - s为周期分段,如果两端不对称或某一段大于str[s..s1](因为是第一次出现大于str[s..s1]的情况,这一段一定在两端输出完毕后输出),注意输出时只输出完整周期(比如str = "DBBDC AABC BCDBBD",当红色字符不相等时,输出"DBB"就行了,后面的"DC"不能输出)。其实这和前面的简单情况类似,只不过前面只比较单个字符和首字符的大小,现在比较一段字符串和首字符串的大小。
#include
#include
#include
#include
using namespace std;
const int MAX_N = 30000 + 2;
const char NEW_LINE = '\n';
char str[MAX_N];
int cnt = 0;
void print(char ch)
{
putchar(ch);
if ((++cnt) % 80 == 0) {
putchar(NEW_LINE);
}
}
void print(int &s, int t)
{
if (s < t) {
while (s < t) {
print(str[s++]);
}
}
else {
while (s > t) {
print(str[s--]);
}
}
}
void solve(int s, int t)
{
while (s <= t) {
int tmps = s;
int tmpt = t;
if (s == t) {
print(str[s]);
return;
}
if (str[s] < str[t]) {
print(str[s++]);
}
else if (str[s] > str[t]) {
print(str[t--]);
}
else { //str[s] == str[t]
int s1 = s + 1;
int t1 = t - 1;
while (s1 < t && str[s1] < str[s] && str[s1] == str[t1]) {
++s1;
--t1;
}
if (s1 == t) {
print(s, t + 1);
return;
}
if (str[s1] == str[s]) {
if (str[s1] != str[t1]) {
print(t, t1);
}
else { //str[s1] == str[t1] == str[s]
bool flag = true;
int s2 = s1++, t2 = t1--;
int T = s2 - s;
while (s1 < t && str[s1] <= str[s] && str[s1] == str[t1]) {
if (flag) {
if (str[s1] < str[s1 - T]) {
flag = false;
}
else if (str[s1] > str[s1 - T]) {
break;
}
}
++s1;
--t1;
}
if (flag) {
s2 = s + (s1 - s) / T * T;
t2 = t - (s1 - s) / T * T;
}
if (s1 == t) {
print(s, s2);
}
if (str[s1] > str[s]) {
print(t, t2);
}
else if (str[s1] > str[t1]) {
print(t, t2);
}
else {
print(s, s2);
}
}
}
else if (str[s1] > str[s] || str[s1] > str[t1]) {
print(t, t1);
}
else { //str[s1] < str[t1]
print(s, s1);
}
}
}
}
int main(int argc, char *argv[])
{
// freopen("D:\\in.txt", "r", stdin);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("\n%c", str + i);
}
solve(0, n - 1);
if (n % 80) {
cout << endl;
}
return 0;
}