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 1102. Strange Dialog

Who can help me?I use graph and search from the end,but WA.
Posted by kkkkk 30 Jul 2004 19:59
const letter:set of char=['a'..'z'];
      max=100000;
var n,t,l,e:longint;
    c:char;
    q,start:boolean;
    s:string;
    sz:array[1..max]of 0..4;{0:space;1:on;2:put;3:out;4:in}
begin
     readln(n);
     for t:=1 to n do
         begin
              fillchar(sz,sizeof(sz),0);
              q:=true;
              start:=true;
              l:=0;
              s:='';
              e:=0;
              while not eoln do
                    begin
                         read(c);
                         if c in letter then
                            begin
                                 start:=false;
                                 s:=s+c;
                                 inc(l);
                                 if l>=6 then
                                    begin
                                         q:=false;
                                         break;
                                    end;
                                 if s='e' then
                                    begin
                                         if sz[e]=1 then
                                            begin
                                                 l:=0;
                                                 s:='';
                                                 sz[e]:=0;
                                                 dec(e);
                                            end
                                                    else
                                            begin
                                                 q:=false;
                                                 break;
                                            end;
                                    end
                                          else
                                 if (l=2)or(l=3) then
                                 begin
                                    if s='on' then
                                       begin
                                            l:=0;
                                            s:='';
                                            inc(e);
                                            sz[e]:=1;
                                       end
                                              else
                                       if s='put' then
                                          begin
                                               l:=0;
                                               s:='';
                                               inc(e);
                                               sz[e]:=2;
                                          end
                                                  else
                                          if s='out' then
                                             begin
                                                  l:=0;
                                                  s:='';
                                                  inc(e);
                                                  sz[e]:=3;
                                             end
                                                     else
                                             if s='in' then
                                                begin
                                                     l:=0;
                                                     s:='';
                                                     inc(e);
                                                     sz[e]:=4;
                                                end;
                                 end;
                            end
                                         else
                            if not start then
                               begin
                                    q:=false;
                                    break;
                               end;
                    end;
              if l<>0 then
                 q:=false;
              l:=e;
              if q then
              while l>0 do
                    begin
                    if (sz[l]=4)or(sz[l]=3) then
                       begin
                            l:=l-1;
                       end
                                            else
                       if sz[l]=2 then
                          begin
                               if (sz[l-1]=4)or(sz[l-1]=3) then
                                  l:=l-2
                                                           else
                                  begin
                                       q:=false;
                                       break;
                                  end;
                          end
                                  else
                          if sz[l]=1 then
                             begin
                                  if sz[l-1]=2 then
                                     l:=l-2
                                               else
                                     begin
                                          q:=false;
                                          break;
                                     end;
                             end;
                      end;
              readln;
              if q then
                 writeln('YES')
                   else
                 writeln('NO');
         end;
end.