題解:
貪心,頭尾指針枚舉字符串兩邊,選小的那個,若相等,則往內延伸判斷哪邊裏面的字符更小
參考程序:
var n,l,r,i,t1,t2,bz:longint;
s,s2:ansistring;
c:char;
begin
readln(n);
for i:=1 to n do
begin
readln(c);
s:=s+c;
if (i>1)and(s[i]<>s[i-1]) then bz:=1;
end;
if bz=0 then
begin
for i:=1 to length(s) do
begin
write(s[i]);
if i mod 80=0 then writeln;
end;
exit;
end;
l:=1;
r:=length(s);
while l<r do
begin
if s[l]<s[r] then
begin
s2:=s2+s[l];
inc(l);
end else
if s[r]<s[l] then
begin
s2:=s2+s[r];
dec(r);
end else
begin
t1:=l;
t2:=r;
while s[t1]=s[t2] do
begin
inc(t1);
dec(t2);
end;
if s[t1]<s[t2] then
begin
s2:=s2+s[l];
inc(l);
end else
begin
s2:=s2+s[r];
dec(r);
end;
end;
end;
s2:=s2+s[l];
for i:=1 to length(s2) do
begin
write(s2[i]);
if i mod 80=0 then writeln;
end;
end.