ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1186. Chemical Reactions

WA on #1, where am I wrong?
Posted by Danica Porobic 1 Jul 2004 18:18
type
  TArray=array[1..1000] of integer;
var
  code:array[1..1000] of string;
  num,temp:TArray;
  i,n,nc:integer;
  s,t:string;
function match(a,b:TArray):Boolean;
var
  i:integer;
  ok:Boolean;
begin
  ok:=true;
  for i:=1 to nc do
    ok:=ok and (a[i]=b[i]);
  match:=ok
end;
procedure init(var a:TArray);
begin
  fillchar(a,sizeof(a),0)
end;
function decode(s:string):integer;
var
  i:integer;
begin
  i:=0;
  repeat
    inc(i)
  until (i=nc+1) or (s=code[i]);
  if i=nc+1 then
    begin
      inc(nc);
      code[nc]:=s
    end;
  decode:=i
end;
procedure analyze(s:string;var a:TArray);
var
  i,mul,count:integer;
  sp,c:string;
begin
  init(a);
  repeat
    if pos('+',s)>0 then
      begin
        sp:=copy(s,1,pos('+',s)-1);
        delete(s,1,pos('+',s))
      end
    else
      begin
        sp:=s;
        s:=''
      end;
        mul:=0;
        i:=1;
        while ('0'<=sp[i]) and (sp[i]<='9') do
          begin
            mul:=10*mul+ord(sp[i])-ord('0');
            inc(i)
          end;
        if mul=0 then
          mul:=1;
        while i<=length(sp) do
          begin
            if (sp[i]='(') or (sp[i]=')') then
              inc(i)
            else
            if ('A'<=sp[i]) and (sp[i]<='Z') then
              begin
                c:=sp[i];
                inc(i);
                if ('a'<=sp[i]) and (sp[i]<='z') then
                  begin
                    c:=c+sp[i];
                    inc(i)
                  end;
                while (sp[i]='(') or (sp[i]=')') do
                  inc(i);
                count:=0;
                while ('0'<=sp[i]) and (sp[i]<='9') do
                  begin
                    count:=10*count+ord(sp[i])-ord('0');
                    inc(i)
                  end;
                if count=0 then
                  count:=1;
                a[decode(c)]:=a[decode(c)]+mul*count
              end
          end
  until s='';
end;
begin
  assign(input,'');
  reset(input);
  readln(s);
  nc:=0;
  analyze(s,num);
  readln(n);
  for i:=1 to n do
    begin
      readln(t);
      analyze(t,temp);
      if match(num,temp) then
        writeln(s,'==',t)
      else
        writeln(s,'!=',t)
    end;
  close(input);
end.