fork download
  1. program parentesi2;
  2. Uses sysutils;
  3. {$H+}
  4. const MAXN=10010;
  5. var
  6. N, i, pos, pos1, lungh,totmodipossibili,tot: 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. procedure conta (stringa:array of char;id:longint);
  15.  
  16. begin
  17. write(stringa[id]); ;
  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.  
  39. (*quello che rimane sono parentesi non bilanciate *)
  40. lungh:=pos1+1;
  41. if lungh=0 then verificabilanciamento:=true
  42. else verificabilanciamento:=false;
  43. if verificabilanciamento=true then totmodipossibili:=totmodipossibili+1;
  44.  
  45. end;
  46.  
  47. if stringa[id]='{' then conta(stringa,id+1);
  48. if stringa[id]='}' then conta(stringa,id+1);
  49. if stringa[id]='*' then
  50. begin
  51. stringa[id]:='{' ; conta(stringa,id+1);
  52. stringa[id]:='}' ; conta(stringa,id+1);
  53. stringa[id]:='*' ;
  54. end;
  55.  
  56.  
  57. end;
  58. begin
  59. (*assign(input, 'input.txt'); reset(input);
  60.   assign(output, 'output.txt'); rewrite(output);*)
  61. ReadLn(N);
  62. readln(ST);
  63. (*uso un array per memorizzare la posizione iniziale delle parentesi aperte e chiuse nella espressione*)
  64. for i:=0 to N-1 do E[i]:=ST[i+1];
  65. totmodipossibili:=0; tot:=0;
  66. conta(E,0);
  67. writeln(totmodipossibili);
  68. end.
  69.  
Success #stdin #stdout 0s 5284KB
stdin
6
{{}**}
stdout
{{}**}}*}}2