- 笨笨的单词排序
- 2014-11-06 18:21:41 @
var
a:array['a'..'z']of integer;
s:array[1..1000]of string;
order:string;
n,k,i:integer;
function tran(p,t:string):boolean;
var
m,n,i:integer;
begin
tran:=false;
m:=length(p);
n:=length(t);
i:=1;
while(i<=m)and(i<=n) do
begin
if p[i]=t[i] then
begin
if(i<>m) and (i<>n) then
inc(i)
else begin
if i=m then begin tran:=true; exit; end;
if i=n then begin tran:=false; exit; end;
end;
end
else if a[p[i]]<a[t[i]] then
begin
tran:=true;
exit;
end
else exit;
end;
end;
procedure qsort(l,r:integer);
var
i,j:integer;
t,m:string;
begin
i:=l;
j:=r;
m:=s[(l+r) div 2];
repeat
while tran(s[i],m) do inc(i);
while not tran(s[j],m) do dec(j);
if i<j then begin
t:=s[j];
s[j]:=s[i];
s[i]:=t;
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
begin
readln(order);
for i:=1 to 26 do
a[order[i]]:=i;
readln(n);
for i:=1 to n do
readln(s[i]);
readln(k);
qsort(1,n);
if k=1 then for i:=1 to n do writeln(s[i]);
if k=0 then for i:=n downto 1 do writeln(s[i]);
end.