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 1027. D++ Again

TL.Thencks
Posted by I am david. Tabo. 23 Dec 2002 22:17
program DPP_AGAIN;
var  mass,stack:array[1..10001] of char;
     kol,ls,i:integer;
     res,comment,cont:boolean;
function arithm:boolean;
begin
if ((ord(mass[i])>=48) and (ord(mass[i])<=57))or(mass[i]='/')or (mass
[i]='*')or(mass[i]='+') or (mass[i]='-')or (mass[i]='(') or (mass[i]
='=') then
        arithm:=true else
        arithm:=false;
end;
begin
{     assign(input,'input.txt');
     reset(input);
     assign(output,'output.txt');
     rewrite(output);}
     fillchar(mass,sizeof(mass),#0);
     fillchar(stack,sizeof(stack),#0);
     kol:=0;
     ls:=0;
     res:=true;
     while not eof do
     begin
          while not eoln do
          begin
               inc(kol);
               read(mass[kol]);
          end;
          readln;
     end;
i:=1;
while i<=kol do
   begin
     if mass[i]='(' then
       begin
          {if begin of comment}
          if mass[i+1]='*' then
           begin
               i:=i+2;
 while (not ((mass[i]='*') and (mass[i+1]=')'))) and (i<=kol) do
        inc(i);
             if i>=kol+1 then
                  res:=false else
           inc(i);
         end else
       {if begin of arithmetic expression}
          begin
            comment:=false;
             inc(i);
              stack[1]:='(';
                    ls:=1;
            cont:=true;
            while cont do
               begin
                 if (mass[i]='(') and (mass[i+1]='*') then
                         begin
                              comment:=true;
                              inc(i);
              end;
                         if not comment then
                         begin
                              if mass[i]='(' then
                              begin
                                   inc(ls);
                                   stack[ls]:='(';
                              end;
                              if (mass[i]=')') then
                              begin
                                 if stack[ls]='(' then
                                   dec(ls) else
                                  res:=false;
                              end else
                              if not arithm then
                                 res:=false;
                         end;
                         if (mass[i]=')') and (mass[i-1]='*') then
                           comment:=false;
                         if (i>=kol) then
                           if (comment) or (ls>0) then
                              res:=false;
                         if (ls=0) or (i>=kol) then
                           cont:=false else
                           inc(i);
                    end;
               end;
          end else
          if mass[i]=')' then
            res:=false;
          if not res then
            i:=kol;
     end;
     if res then
       writeln('YES') else
       writeln('NO');
end.