Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
在Ural上过了,POJ上总是WA,是不是SPJ的问题?here's my code: var str:string; n,L,i,j,k:integer; f:array[1..100,1..100]of integer; G:array[1..100,1..100]of string; function match(c:char):string; begin if (c='(')or(c=')')then exit('()'); if (c='[')or(c=']')then exit('[]'); end; function pair(c1,c2:char):boolean; begin if (c1='(')and(c2=')')then exit(true); if (c1='[')and(c2=']')then exit(true); exit(false); end; begin readln(str); n:=length(str); if n=0 then exit; for i:=1 to n do for j:=i to n do f[i,j]:=100; for L:=1 to n do for i:=1 to n-L+1 do begin j:=i+l-1; if i=j then begin f[i,j]:=1; g[i,j]:=match(str[i]); end else begin if (str[i]='(')and(f[i,j]>=f[i+1,j]+1)then begin f[i,j]:=f[i+1,j]+1; g[i,j]:='('+g[i+1,j]+')'; end; if (str[i]='[')and(f[i,j]>=f[i+1,j]+1)then begin f[i,j]:=f[i+1,j]+1; g[i,j]:='['+g[i+1,j]+']'; end; if (str[j]=')')and(f[i,j]>=f[i,j-1]+1)then begin f[i,j]:=f[i,j-1]+1; g[i,j]:='('+g[i,j-1]+')'; end; if (str[j]=']')and(f[i,j]>=f[i,j-1]+1)then begin f[i,j]:=f[i,j-1]+1; g[i,j]:='['+g[i,j-1]+']'; end; if (pair(str[i],str[j]))and(f[i,j]>=f[i+1,j-1])then begin f[i,j]:=f[i+1,j-1]; g[i,j]:=str[i]+g[i+1,j-1]+str[j]; end; for k:=i+1 to j do if f[i,j]>=f[i,k-1]+f[k,j] then begin f[i,j]:=f[i,k-1]+f[k,j]; g[i,j]:=g[i,k-1]+g[k,j]; end; end; end; writeln(g[1,n]); end. Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator