about summary refs log tree commit diff
path: root/2ndary/12/TP-HN-2009/R1/BTN.PAS
blob: dbb022da7167453ec10b95af72d1a88131f95fe2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
type sni = record
  s : ansistring;
  n : integer
end;

var
  f : text;
  s : ansistring;
  op, cl : integer;
  c : char;

function cal(s : ansistring) : integer;
  var
    c : char;
    tmp : integer = 0;
  begin
    cal := 0;
    for c in s do
      if c = '(' then
        begin
          inc(tmp);
          if tmp > cal then
            cal := tmp
        end
      else
        begin
          dec(tmp);
          if tmp < 0 then exit(0)
        end;
    if tmp <> 0 then
      exit(0)
  end;

function rplc(
  s : ansistring;
  c : char;
  idx : integer
) : ansistring;
  begin
    exit(copy(s, 1, idx - 1) + c + copy(s, idx + 1, length(s) - idx + 1))
  end;

function libtn(
  s : ansistring;
  op, cl, idx : integer
) : sni;
  var
    i : integer;
    v0, v1 : sni;
  begin
    if (op = 0) and (cl = 0) then
      begin
        libtn.s := s;
        libtn.n := cal(s);
        exit
      end;
    i := idx;
    while s[i] <> '?' do
      inc(i);
    if op = 0 then
      exit(libtn(rplc(s, ')', i), 0, cl - 1, i + 1));
    if cl = 0 then
      exit(libtn(rplc(s, '(', i), op - 1, 0, i + 1));
    v0 := libtn(rplc(s, '(', i), op - 1, cl, i + 1);
    v1 := libtn(rplc(s, ')', i), op, cl - 1, i + 1);
    if v0.n > v1.n then
      exit(v0)
    else exit(v1)
  end;

begin
  assign(f, 'BTN.INP');
  reset(f);
  read(f, s);
  close(f);
  op := length(s) div 2;
  cl := length(s) div 2;
  for c in s do
    if c = '(' then
      dec(op)
    else if c = ')' then
      dec(cl);
  assign(f, 'BTN.OUT');
  rewrite(f);
  writeln(f, libtn(s, op, cl, 1).s);
  close(f)
end.