天天看點

poj 3617 Best Cow Line(貪心)



best cow line

time limit: 1000ms

memory limit: 65536k

total submissions: 8579

accepted: 2629

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

sample output

source

這道題的思路算是比較簡單吧,算是水題,但是越是簡單的題越能暴露出很多問題。。(基本功不紮實啊,開始wa了好幾次,後來就一直pe,開始是沒看清楚題目,後來才發現自己有幾處細節沒掌握好),字元串的處理問題還有很多細節要注意啊!!!

把這道題的思路簡單說一下吧:就是不斷的比較s串的開頭和末尾較小的一個字元,較小的就可以輸出了,(這裡有一個相等的情況),如果相等的話就要比較他們之後的字元(較小的那一個),思路并不複雜。要看清題意啊,開始的輸入是一行一行的輸入一個字元,後面輸出的時候要80個字元要換行

下面是我的代碼:

一個這樣的題目都還ac我好久啊,基礎還是不紮實,自己還是要加強訓練!!!加油!!