记录编号 147786 评测结果 WAWAWAWWWWWAA
题目名称 括号序列 最终得分 38
用户昵称 Gravatarhjt 是否通过 未通过
代码语言 Pascal 运行时间 0.023 s
提交时间 2015-02-03 23:16:14 内存使用 0.63 MiB
显示代码纯文本
program bracket;
var s:string;
    le,i,j,k,h:longint;
    f,lay,sp:array[0..200,0..200]of longint;

function max(x,y:longint):longint;
begin
  if x>y then exit(x)
  else exit(y);
end;

procedure print(x,y:longint);
begin
  if x>y then exit;
  if x=y then
    begin
      write(s[x]);
      if s[x]='(' then write(')');
      if s[x]='[' then write(']');
      exit;
    end;
  if x<y then
    if sp[x,y]=0 then
      begin
        write(s[x]);
        print(x+1,y-1);
        write(s[y]);
      end
    else
      begin
        print(x,sp[x,y]);
        print(sp[x,y]+1,y);
      end;
end;

begin
  assign(input,'bracket.in');
  assign(output,'bracket.out');
  reset(input);
  rewrite(output);
  fillchar(f,sizeof(f),$5f);
  fillchar(sp,sizeof(sp),255);
  readln(s);
  le:=length(s);
  for i:=1 to le do
    begin
      f[i,i]:=1;
      f[i+1,i]:=0;
      lay[i,i]:=1;
    end;
  for k:=2 to le do
    for i:=1 to le-k+1 do
      begin
        j:=i+k-1;
        for h:=i to j-1 do
          if f[i,j]>f[i,h]+f[h+1,j] then
            begin
              f[i,j]:=f[i,h]+f[h+1,j];
              lay[i,j]:=max(lay[i,h],lay[h+1,j]);
              sp[i,j]:=h;
            end
          else if f[i,j]=f[i,h]+f[h+1,j] then
                 if lay[i,j]>max(lay[i,h],lay[h+1,j]) then
                   begin
                     lay[i,j]:=max(lay[i,h],lay[h+1,j]);
                     sp[i,j]:=h;
                   end;
        if ((s[i]='(') and (s[j]=')')) or ((s[i]='[') and (s[j]=']')) then
          if f[i,j]>f[i+1,j-1] then
            begin
              f[i,j]:=f[i+1,j-1];
              lay[i,j]:=lay[i+1,j-1]+1;
              sp[i,j]:=0;
            end
          else if f[i,j]=f[i+1,j-1] then
                 if lay[i,j]>lay[i+1,j-1]+1 then
                   begin
                     lay[i,j]:=lay[i+1,j-1]+1;
                     sp[i,j]:=0;
                   end;
      end;
  //writeln(f[1,le]);
  print(1,le);
  close(input);
  close(output);
end.