POJ-3617 Best Cow Line

POJ-3617 Best Cow Line

七月 31, 2022

题目

Description

FJ is about to take his N (1 ≤ N ≤ 2,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

1
2
3
4
5
6
7
6
A
C
D
B
C
B

Sample Output

1
ABCBCD

分析

很显然,题目的解决方法是贪心。我为了字典顺序小,我肯定从头和尾抽一个小的填进来。问题就在于头和尾相等的时候。

在第一次尝试时,我尝试将两者都用上,用头的做一种情况,用尾的做一种情况。这样就有很多个字符串了,再sort一下就可以。所以开始用一点递归。后来在提交的时候不是超出内存就是超出了时间限制。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void connect(string str,int start,int end){
if(start==end){
str+=origin_str[start];
strs.push_back(str);
return;
}
if(origin_str[start]<origin_str[end]){
str+=origin_str[start];
connect(str,start+1,end);
}else if(origin_str[start]>origin_str[end]){
str+=origin_str[end];
connect(str,start,end-1);
}else if(origin_str[start]==origin_str[end]){
str+=origin_str[start];
connect(str,start+1,end);
connect(str,start,end-1);
}
}

所以,在相等的时候,肯定只要判断相等的后面,哪个相等取走后利益高,就用哪个。所以向中判断。让程序判断取走后的下一个,下下一个……这些情况哪些好就可以了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void connect(string str,int start,int end){
if(start==end){
str+=origin_str[start];
if(best_str=="") best_str=str;
else if(str<best_str) best_str=str;
return;
}
if(origin_str[start]<origin_str[end]){
str+=origin_str[start];
connect(str,start+1,end);
}else if(origin_str[start]>origin_str[end]){
str+=origin_str[end];
connect(str,start,end-1);
}else if(origin_str[start]==origin_str[end]){
str+=origin_str[start];
string left="",right="";
for(int i=start+1,j=end-1;i<j;i++,j--){
left+=origin_str[i];
right+=origin_str[j];
}
if(left<right){
connect(str,start+1,end);
}else connect(str,start,end-1);
}
}

可惜这样还是超时了,然后试图用一下substr,诶,就过了。就是substr的长度要判断好。

实际上不需要也不能从中间切开,直接提取去从头到尾的和反转的从头到尾就可以了。我脑子有点抽。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void connect(string str,int start,int end){
if(start==end){
str+=origin_str[start];
if(best_str=="") best_str=str;
else if(str<best_str) best_str=str;
return;
}
if(origin_str[start]<origin_str[end]){
str+=origin_str[start];
connect(str,start+1,end);
}else if(origin_str[start]>origin_str[end]){
str+=origin_str[end];
connect(str,start,end-1);
}else if(origin_str[start]==origin_str[end]){
str+=origin_str[start];
string left="",right="";
left=origin_str.substr(start+1,(end-start)/2);
if((end+start)%2==0) right=origin_str.substr((start+end)/2,(end-start)/2);
else right=origin_str.substr((start+end)/2+1,(end-start)/2);
reverse(right.begin(),right.end());
if(left<right){
connect(str,start+1,end);
}else connect(str,start,end-1);
}
}

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int length;
string origin_str="";
string best_str="";
void connect(string str,int start,int end){
if(start==end){
str+=origin_str[start];
if(best_str=="") best_str=str;
else if(str<best_str) best_str=str;
return;
}
if(origin_str[start]<origin_str[end]){
str+=origin_str[start];
connect(str,start+1,end);
}else if(origin_str[start]>origin_str[end]){
str+=origin_str[end];
connect(str,start,end-1);
}else if(origin_str[start]==origin_str[end]){
str+=origin_str[start];
string left="",right="";
left=origin_str.substr(start+1,(end-start)/2);
if((end+start)%2==0) right=origin_str.substr((start+end)/2,(end-start)/2);
else right=origin_str.substr((start+end)/2+1,(end-start)/2);
reverse(right.begin(),right.end());
if(left<right){
connect(str,start+1,end);
}else connect(str,start,end-1);
}
}
int main(){
// freopen("TrueExample.txt","r",stdin);
cin >> length;
for(int abc=0;abc<length;abc++){
char c;
cin >> c;
origin_str+=c;
}
connect("",0,length-1);
for(int i=0;i<best_str.size();i++){
cout << best_str[i];
if((i+1)%80==0) cout << endl;
}
return 0;
}

其他

阴间输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
333
D
A
C
C
C
E
D
D
E
E
C
C
A
D
E
B
B
C
C
D
E
E
D
D
D
B
E
D
A
E
C
B
A
E
C
B
E
E
B
B
A
E
C
A
B
C
E
A
C
A
B
D
A
E
E
A
A
D
C
E
C
D
D
C
E
A
B
B
C
A
A
C
B
A
C
A
A
C
A
B
A
E
D
C
D
A
D
E
A
B
C
E
D
C
C
B
E
D
A
C
D
E
D
C
B
E
B
C
B
C
D
D
C
D
A
E
C
B
E
E
B
E
D
C
D
A
D
D
B
D
B
C
B
E
B
B
C
E
A
B
D
C
D
E
D
E
E
D
C
E
D
E
E
E
C
C
C
D
E
D
B
C
B
A
D
A
A
B
C
D
B
B
E
E
D
B
B
E
D
C
C
C
C
D
C
E
A
B
C
B
E
E
D
B
B
A
B
C
B
A
A
D
B
E
E
D
A
A
D
D
A
A
A
B
E
C
A
A
B
A
D
C
A
D
D
A
A
D
A
E
D
D
D
C
C
B
D
C
A
C
E
E
C
C
D
B
E
D
C
C
D
C
B
A
E
D
E
E
A
B
E
D
C
D
E
D
E
A
C
E
E
A
A
E
C
A
E
B
A
D
B
B
C
B
C
C
E
A
B
D
D
B
C
B
D
D
E
E
A
E
A
C
A
E
B
D
E
E
C
A
B
B
A
A
E
E
D
C
B
E
E
B
B
C
E
E
B
E
E
E
C
B
E

输出

1
2
3
4
5
DACCCEBCEDDEECCADEBBCCDEEDDDBEDAECBAECBEEBBAECABCEACABDAEEAADCECDDCEABBCAACBACAA
CABAEDCDADEABCEDCCBEDACDEDCBEBCBCDDCDAECBEEBEDCDADDBDBCBEBBCEABDCDEDEEDCEDEEEBEE
CBBEEBCDEEAABBACEEDBEACAEAEEDDBCBDDBAECCBCBBDABEACEAAEECAEDEDCDEBAEEDEABCDCCDEBD
CCEECACDBCCDDDEADAADDACDABAACEBAAADDAADEEBDAABCBABBDEEBCBAECDCCCCDEBBDEEBBDCBAAD
ABCBDEDCCCEEE