ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1186. Chemical Reactions

Why I get wrong?
Posted by sunshine 5 Sep 2002 15:38
I did it like this:
Why I get wrong?

type
td=array[1..26,0..26]of integer;
tt=record
a,b:integer;
x:array[1..2]of integer;
xi:integer;
ty:integer;
d:td;
end;
var
s1,s2:string;
i,n:integer;
d1,d2:td;
t:array[1..50]of ^tt;
procedure turn(s:string;var d:td);
var
i,o,p:integer;
k:integer;
begin
k:=1;
new(t[k]);
fillchar(t[k]^,sizeof(t[k]^),0);
t[k]^.x[1]:=1;
t[k]^.x[2]:=1;
s:=s+'.';
for i:=1 to length(s) do
begin
with t[k]^ do
begin
if s[i] in ['(',')','.','A'..'Z','+'] then
begin
if ty=1
then d[a,b]:=d[a,b]+x[1]*x[2];
if ty=2
then begin
for o:=1 to 26 do
for p:=0 to 26 do
d[o,p]:=d[o,p]+t[k+1]^.d[o,p]*x[2]*x[1];
dispose(t[k+1]);
end;
x[1]:=1;
xi:=0;
end;
case s[i] of
'A'..'Z':begin
ty:=1;
a:=byte(s[i])-byte('A')+1;
b:=0;
end;
'a'..'z':begin
b:=byte(s[i])-byte('a')+1;
end;
'0'..'9':begin
if xi=0 then
begin
if a=0
then xi:=2
else xi:=1;
x[xi]:=byte(s[i])-byte('0');
end
else x[xi]:=x[xi]*10+byte(s[i])-byte('0');
end;
'(':begin
ty:=2;
inc(k);
new(t[k]);
fillchar(t[k]^,sizeof(t[k]^),0);
t[k]^.x[1]:=1;
t[k]^.x[2]:=1;
end;
')':begin
dec(k);
end;
'+':begin
a:=0;
b:=0;
x[1]:=1;
x[2]:=1;
xi:=0;
ty:=0;
end;
end;
end;
end;
delete(s,length(s),1);
d:=t[k]^.d;
end;
function bj(var d1,d2:td):boolean;
var
i,j:integer;
begin
for i:=1 to 26 do
for j:=0 to 26 do
if d1[i,j]<>d2[i,j] then
begin
bj:=false;
exit;
end;
bj:=true;
end;
begin
turn(s1,d1);
for i:=1 to n do
begin
turn(s2,d2);
if bj(d1,d2)
then writeln(s1,'==',s2)
else writeln(s1,'!=',s2)
end;
end.
Re: Why I get wrong?
Posted by linchuan 8 Sep 2002 13:14
I don't know!!!
>
> type
>   td=array[1..26,0..26]of integer;
>   tt=record
>     a,b:integer;
>     x:array[1..2]of integer;
>     xi:integer;
>     ty:integer;
>     d:td;
>   end;
> var
>   s1,s2:string;
>   i,n:integer;
>   d1,d2:td;
>   t:array[1..50]of ^tt;
> procedure turn(s:string;var d:td);
>   var
>     i,o,p:integer;
>     k:integer;
>   begin
>     k:=1;
>     new(t[k]);
>     fillchar(t[k]^,sizeof(t[k]^),0);
>     t[k]^.x[1]:=1;
>     t[k]^.x[2]:=1;
>     s:=s+'.';
>     for i:=1 to length(s) do
>       begin
>         with t[k]^ do
>           begin
>             if s[i] in ['(',')','.','A'..'Z','+'] then
>               begin
>                 if ty=1
>                   then d[a,b]:=d[a,b]+x[1]*x[2];
>                 if ty=2
>                   then begin
>                     for o:=1 to 26 do
>                       for p:=0 to 26 do
>                         d[o,p]:=d[o,p]+t[k+1]^.d[o,p]*x[2]*x[1];
>                     dispose(t[k+1]);
>                   end;
>                 x[1]:=1;
>                 xi:=0;
>               end;
>             case s[i] of
>               'A'..'Z':begin
>                 ty:=1;
>                 a:=byte(s[i])-byte('A')+1;
>                 b:=0;
>               end;
>               'a'..'z':begin
>                 b:=byte(s[i])-byte('a')+1;
>               end;
>               '0'..'9':begin
>                 if xi=0 then
>                   begin
>                     if a=0
>                       then xi:=2
>                       else xi:=1;
>                     x[xi]:=byte(s[i])-byte('0');
>                   end
>                   else x[xi]:=x[xi]*10+byte(s[i])-byte('0');
>               end;
>               '(':begin
>                 ty:=2;
>                 inc(k);
>                 new(t[k]);
>                 fillchar(t[k]^,sizeof(t[k]^),0);
>                 t[k]^.x[1]:=1;
>                 t[k]^.x[2]:=1;
>               end;
>               ')':begin
>                 dec(k);
>               end;
>               '+':begin
>                 a:=0;
>                 b:=0;
>                 x[1]:=1;
>                 x[2]:=1;
>                 xi:=0;
>                 ty:=0;
>               end;
>             end;
>           end;
>       end;
>     delete(s,length(s),1);
>     d:=t[k]^.d;
>   end;
> function bj(var d1,d2:td):boolean;
>   var
>     i,j:integer;
>   begin
>     for i:=1 to 26 do
>       for j:=0 to 26 do
>         if d1[i,j]<>d2[i,j] then
>           begin
>             bj:=false;
>             exit;
>           end;
>     bj:=true;
>   end;
> begin