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 |
懂java的进import java.io.*; import java.util.*; public class Main { static String str; static int d[][]; static int n; public static void main(String arg[]) throws Exception { BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in)); String s=buffer.readLine(); str=" "+s; n=str.length()-1; d=new int[n+1][n+1]; //System.out.println(((str.charAt(2)=='('&str.charAt(3)==')')|(str.charAt(2) =='['&str.charAt(3)==']'))); int res=bracket(); System.out.println(res); n=n-1;d=new int[n+1][n+1]; StringBuffer sb=new StringBuffer(s); int len=s.length(); int slen=0; for(int l=0;l<len;l++) { d=new int[n+1][n+1]; str=" "+sb.deleteCharAt(l); if(bracket()<res) { if(s.charAt(slen)=='('||s.charAt(slen)==')') {sb=sb.insert(l,"()");} else if(s.charAt(slen)=='['||s.charAt(slen)==']') sb=sb.insert(l,"[]"); res--; l++; n++; len++; } else{sb.insert(l,s.charAt(slen));} slen++; } System.out.print(sb.toString()); } public static int bracket() { int j=0; for(int i=1;i<n;i++)d[i][i-1]=0; for(int i=1;i<=n;i++)d[i][i]=1; for(int p=1;p<=n-1;p++) { for(int i=1;i<=n-p;i++) { j=i+p; d[i][j]=100; if((str.charAt(i)=='('&str.charAt(j)==')')|(str.charAt(i)=='['&str.charAt (j)==']')) {d[i][j]=Math.min(d[i][j],d[i+1][j-1]);} if(str.charAt(i)=='('||str.charAt(i)=='[') {d[i][j]=Math.min(d[i][j],d[i+1][j]+1);} if(str.charAt(j)==')'||str.charAt(j)==']') {d[i][j]=Math.min(d[i][j],d[i][j-1]+1);} for(int k=i;k<=j-1;k++) {d[i][j]=Math.min(d[i][j],d[i][k]+d[k+1][j]);} } } //for(int i=0;i<=n;i++) //{for( j=0;j<=n;j++)System.out.print(d[i][j]+" "); System.out.println();} return d[1][n]; } } //谁帮我编译一下,怎么老报错? Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator