fork download
  1. program parentesi2;
  2. Uses sysutils;
  3. {$H+}
  4. const MAXN=10010;
  5. var
  6. N, i, pos, pos1, lungh,totmodipossibili: LongInt;
  7. ST:Ansistring;
  8. E: array[0..MAXN] of char;
  9. E1,stringa1: array[0..MAXN] of char;
  10. indice :array[0..MAXN] of longint;
  11. p:char;
  12. verificabilanciamento:boolean;
  13.  
  14. function conta (stringa:array of char;id:longint):longint;
  15.  
  16. begin
  17. write (id,' ',totmodipossibili,' ');
  18. if id=N then
  19. begin
  20. (*leggo e verifico se è bilanciata*)
  21. pos1:=-1;
  22. pos:=-1;
  23. while (pos <N-1) do
  24. begin
  25. pos:=pos+1;
  26. p:=stringa[pos];
  27. if p='{' then
  28. begin
  29. pos1:=pos1+1;
  30. stringa1[pos1]:=p;
  31. indice[pos1]:=pos;
  32. end
  33. else
  34. if ( p='}') then begin if (pos1>=0) and (stringa1[pos1]='{') then pos1:=pos1-1
  35. else begin pos1:=pos1+1; stringa1[pos1]:=p; indice[pos1]:=pos;end;
  36. end;
  37. end;
  38. (*quello che rimane sono parentesi non bilanciate *)
  39. lungh:=pos1+1;
  40. if lungh=0 then verificabilanciamento:=true
  41. else verificabilanciamento:=false;
  42. if verificabilanciamento=true then totmodipossibili:=totmodipossibili+1;
  43. writeln(verificabilanciamento);
  44. end;
  45. writeln(stringa[id-1]);
  46. if stringa[id]='{' then conta(stringa,id+1);
  47. if stringa[id]='}' then conta(stringa,id+1);
  48. if stringa[id]='*' then
  49. begin
  50. stringa[id]:='{' ; conta(stringa,id+1);
  51. stringa[id]:='}' ; conta(stringa,id+1);
  52. stringa[id]:='*' ;
  53. end;
  54.  
  55. conta:=totmodipossibili;
  56. end;
  57. begin
  58. (*assign(input, 'input.txt'); reset(input);
  59.   assign(output, 'output.txt'); rewrite(output);*)
  60. ReadLn(N);
  61. readln(ST);
  62. (*uso un array per memorizzare la posizione iniziale delle parentesi aperte e chiuse nella espressione*)
  63. for i:=0 to N-1 do E[i]:=ST[i+1];
  64. totmodipossibili:=0;
  65. writeln(conta(E,0));
  66. end.
  67.  
Success #stdin #stdout 0.01s 5284KB
stdin
6
{{***}
stdout
0 0 
1 0 {
2 0 {
3 0 {
4 0 {
5 0 {
6 0 FALSE
}
5 0 }
6 0 FALSE
}
4 0 }
5 0 {
6 0 FALSE
}
5 0 }
6 0 TRUE
}
3 1 }
4 1 {
5 1 {
6 1 FALSE
}
5 1 }
6 1 TRUE
}
4 2 }
5 2 {
6 2 TRUE
}
5 3 }
6 3 FALSE
}
3