Advertisement
WadeRollins2710

Khôi phục ngoặc

Jan 17th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.90 KB | None | 0 0
  1. Program Bai077;
  2. Uses crt;
  3. Const
  4.         InputFile = 'BRACKETS.INP';
  5.         OutputFile = 'BRACKETS.OUT';
  6. Var
  7.         f: Text;
  8.         A, Stack: Array [1..10000] of Integer;
  9.         n, TopInd: Integer;
  10.         S: ANSIString;
  11. Procedure Input;
  12.         Var
  13.                 i: Integer;
  14.         Begin
  15.                 Assign(f,InputFile);
  16.                 Reset(f);
  17.                 Readln(f,n);
  18.                 For i:=1 to n do
  19.                         Read(f,A[i]);
  20.                 Close(f);
  21.         End;
  22. Procedure Init;
  23.         Begin
  24.                 TopInd:=0;
  25.         End;
  26. Procedure Push(i: Integer);
  27.         Begin
  28.                 inc(TopInd);
  29.                 Stack[TopInd]:=i;
  30.         End;
  31. Procedure Pop;
  32.         Begin
  33.                 If TopInd>=1 then dec(TopInd);
  34.         End;
  35. Function Top: Integer;
  36.         Begin
  37.                 If TopInd>=1 then Top:=Stack[TopInd]
  38.                 else Top:=-1;
  39.         End;
  40. Procedure DecreaseAll;
  41.         Var
  42.                 i: Integer;
  43.         Begin
  44.                 For i:=1 to TopInd do
  45.                         dec(Stack[i],2);
  46.         End;
  47. Procedure Process;
  48.         Var
  49.                 i: Integer;
  50.         Begin
  51.                 Init;
  52.                 S:='';
  53.                 For i:=1 to n do
  54.                         Begin
  55.                                 Push(A[i]);
  56.                                 S:=S+'(';
  57.                                 While Top=0 do
  58.                                         Begin
  59.                                                 Pop; S:=S+')';
  60.                                                 DecreaseAll;
  61.                                         End;
  62.                         End;
  63.         End;
  64. Procedure Output;
  65.         Begin
  66.                 Assign(f,OutputFile);
  67.                 Rewrite(f);
  68.                 Writeln(f,S);
  69.                 Close(f);
  70.         End;
  71. Begin
  72.         Input;
  73.         Process;
  74.         Output;
  75. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement