SGU 179 — Brackets light

Posted on 六月 8th, 2008.

找到最靠后的一个满足它左边的’('比’)'数量多的’(',改成’)',然后在后面补(…()…)和)…).
好久以前就写过的题.刚才写错了一点,在最后填括号的时候先填(…()…),再填)…).

{
SGU 179; Brackets light
- sqybi’s code
}
//for my winsty
program sgu179_sqybi;
  var
    i, l, r, tr, t: longint;
    s: ansistring;

  begin
    readln(s);
    l := length(s);
    t := 0;
    r := 0;
    for i:=1 to l do begin
      if (t > 0) and (s[i] = ‘(’) then begin
        tr := t;
        r := i;
      end;
      t := t + 1 - 2 * ord(s[i] = ‘)’);
    end;
    if r = 0 then begin
      writeln(’No solution’);
      halt;
    end;
    s[r] := ‘)’;
    l := l - (tr - 1);
    for i:=r+1 to r+(l-r) div 2 do
      s[i] := ‘(’;
    for i:=r+(l-r) div 2+1 to l do
      s[i] := ‘)’;
    for i:=1 to tr-1 do s[l+i] := ‘)’;
    writeln(s);
  end.

阅读(120 次)

Make a Comment

Make A Comment: ( None so far )

blockquote and a tags work here.

Liked it here?
Why not try sites on the blogroll...