Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

懂java的进

Posted by yanlimin9 at 2006-09-17 22:15:15 on Problem 1141
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator