I WA too.And I don't know where I wrote wrong.Here's my code(FP):
const b:array[1..7,1..7]of longint=
((0,0,-1,-1,-1,-1,-1),
(0,0,-1,-1,-1,-1,-1),
(1,1,0,0,-1,-1,-1),
(1,1,0,0,-1,-1,-1),
(1,1,1,1,0,-1,-1),
(1,1,1,1,1,0,-1),
(1,1,1,1,1,1,0));
var s:string;
n,i,l,p,line,lin:longint;
a:array[1..400]of char;
function g(c:char):longint;
begin
case c of
'+':exit(1);
'-':exit(2);
'*':exit(3);
'/':exit(4);
'^':exit(5);
'(':exit(6);
')':exit(7);
end;
end;
begin
readln(line);
for lin:=1 to line do
begin
n:=0;
readln(s);
l:=length(s);
for i:=1 to l do
begin
if s[i] in ['a'..'z'] then
begin
write(s[i]);
continue;
end;
if s[i]='(' then
begin
inc(n);
a[n]:='(';
continue;
end;
if s[i]=')' then
begin
while (n>0)and(a[n]<>'(') do
begin
write(a[n]);
dec(n);
end;
dec(n);
continue;
end;
p:=g(s[i]);
if (n=0)or(b[p,g(a[n])]=1) then
begin
inc(n);
a[n]:=s[i];
end
else
begin
while (n>0)and((b[p,g(a[n])]=-1)or(b[p,g(a[n])]=0))and(a[n]<>'(') do
begin
write(a[n]);
dec(n);
end;
inc(n);
a[n]:=s[i];
end;
end;
for i:=n downto 1 do write(a[i]);
writeln;
end;
end.
Please hlep find out my mistake.