pku3617 Best Cow Line

有一个字符串,每次只能从两端取,求取得的字典序最小的子符序列。

每次比较剩余字符串的正向和反向字符串的大小,正向大就在尾取,否则在头取,相当于贪心了吧。

View Code
 1 program pku3617(input,output);
2 var
3 s1,s2 : ansistring;
4 answer : ansistring;
5 n : longint;
6 procedure init;
7 var
8 i : longint;
9 ch : char;
10 begin
11 readln(n);
12 s1:='';
13 for i:=1 to n do
14 begin
15 readln(ch);
16 s1:=s1+ch;
17 end;
18 s2:='';
19 for i:=length(s1) downto 1 do
20 s2:=s2+s1[i];
21 end; { init }
22 procedure main;
23 var
24 i : longint;
25 begin
26 answer:='';
27 for i:=1 to n do
28 begin
29 if s1>s2 then
30 begin
31 answer:=answer+s2[1];
32 delete(s2,1,1);
33 delete(s1,length(s1),1);
34 end
35 else
36 begin
37 answer:=answer+s1[1];
38 delete(s1,1,1);
39 delete(s2,length(s2),1);
40 end;
41 end;
42 end; { main }
43 procedure print;
44 var
45 tmp : ansistring;
46 begin
47 while length(answer)>=80 do
48 begin
49 tmp:=copy(answer,1,80);
50 writeln(tmp);
51 delete(answer,1,80);
52 end;
53 if answer<>'' then
54 writeln(answer);
55 end; { print }
56 begin
57 init;
58 main;
59 print;
60 end.



posted @ 2012-03-21 12:20  Codinginging  阅读(316)  评论(0编辑  收藏  举报