|
|
вернуться в форумPlease tell me where have I mistaken! I ve been fighting this problem for almost a week... Thats what the variables mean: Com-whether the viewed symbol is inside a comment. j- number of opening brackets reduced by the number of closing brackets (brackets in comments dont count) sk-whether the previos symbol was '(' zv-whether the previos symbol was '*' =-=-==-=-=-=-=-=--=- Program acm_1027; {D++} Var A :array [1..10000] of char; i,j,k,l :integer; com,Form,Ans :boolean; sk,zv :boolean; c :char; Function Bad(x:char):boolean; var an:boolean; Begin an:=true; If x='=' then an:=false; If x='+' then an:=false; If x='-' then an:=false; If x='*' then an:=false; If x='/' then an:=false; If x='(' then an:=false; If x=')' then an:=false; If x=#13 then an:=false; If x=#10 then an:=false; If (x>='0')and(x<='9') then an:=false; bad:=an; End; Begin ans:=true; i:=0; c:=' '; While (Not Eof) Do Begin Inc(i); Read(A[i]); End; l:=i; j:=0; Com:=False; Form:=False; zv:=false; sk:=false; For i:=1 to L do Begin If (A[i]=')') and (not com) then Dec(j); If (A[i]=')')and(zv) then com:=false; If (A[i]='*')and(sk) then com:=true; If (sk) and (not com) then Inc(j); If A[i]='*' then zv:=true Else zv:=false; If A[i]='(' then sk:=true Else sk:=false; If j<0 then ans:=false; If (j>0)and(not com)and(Bad(A[i])) then ans:=false; End; if (j>0)or(com) then ans:=false; If Ans then writeln('YES') Else writeln('NO'); End. Re: Please tell me where have I mistaken! I ve been fighting this problem for almost a week... Try this test ((*)()()()()()()()()())))))))((((((*)()()) it is correct (YES), but your program returns NO, it thinks that (*) is opening and closing comment simultaneously; by the way the test (*) is ambiguous - it could be YES, if you consider it as an arithmetical expression, or it could be NO, if you consider it as an unclosed comment; my program gives NO (it got AC), yours YES, so I reckon that (* is with higher priority than ( Good luck. HELP ! I try it MORE than one week. :( What's wrong with my program? I tried all the testdata such as (*) ->NO () ->YES (()) ->YES and yours ((*)()()()()()()()()())))))))((((((*)()()) ->YES And my program still got WA,I don't why. Could you help me? const MaxSize=10005; var word:array[1..MaxSize] of char; Top,Cur:integer; C:char; Error:boolean; procedure comment; var b:boolean; begin b:=true; inc(cur); while (cur<top) and b do begin if (word[cur]='*') and (word[Cur+1]=')') then b:=false else inc(cur); end; if not b then inc(cur) else error:=true; end; procedure expression; var b:boolean; T:integer; begin inc(Cur); if (Cur>Top) then begin Error:=true; exit; end; //if (word[Cur]=')') then begin error:=true; exit; end; if word[Cur]='*' then begin Comment; exit; end; b:=false; T:=1; while (Not error) and (Cur<=Top) and (T>0) and (word[Cur] in ['+','-','*','/','0'..'9','(',')','=']) do begin if (word[cur]='(') and (word[cur+1]<>'*') then begin b:=true; T:=T+1; end; if (word[cur]<>'(') and (word[cur]<>')') then b:=true; if (word[Cur]=')') then T:=T-1; if (word[Cur]='(') and (word[Cur+1]='*') then begin inc(cur); comment; end; if T<>0 then inc(cur); end; if T<>0 then error:=true; //if not b then error:=true; end; begin
Top:=0; read(c); while not eof do begin if (ord(c)<>13) and (ord(C)<>10) and (ord(c)<>0) then begin inc(Top); word[top]:=c; end; read(c); end; Cur:=1; Error:=false; Word[Top+1]:=' '; while (Not Error) and (Cur<=Top) do begin if word[Cur]='(' then Expression else if word[Cur]=')' then error:=true; inc(Cur); end; if Not error then writeln('YES') else writeln('NO'); end. |
|
|