题解:
贪心,头尾指针枚举字符串两边,选小的那个,若相等,则往内延伸判断哪边里面的字符更小
参考程序:
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.
本文探讨了如何通过特定的队列变换策略,使奶牛队列达到字典序最小的顺序,介绍了问题背景及解决方案,包括贪心算法的具体实现。
1643

被折叠的 条评论
为什么被折叠?



