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

怎样才能不超时?? 谢谢!

Posted by zhb_msqx at 2007-08-18 16:51:16 on Problem 1141
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

char s[100];

string check(const char *c,int start,int end){
	if(start>end)return "";
	if(start==end){
		if(c[start]=='('||c[start]==')')return "()";
		else return "[]";
	}
	string res="";
	int minlen=10000;
	int i=start;
	string str="";
		if(c[i]=='('){
			for(int j=i+1;j<=end;j++){
				if(c[j]==')'){
					str="("+check(c,i+1,j-1)+")"+check(c,j+1,end);
					if(str.length()<=minlen){
						minlen=str.length();
						res=str;
					}
				}
			}
			str="()"+check(c,i+1,end);
			if(str.length()<=minlen){				
				minlen=str.length();
				res=str;
			}
		}else if(c[i]==')'){
			str="()"+check(c,i+1,end);

			if(str.length()<minlen){

				minlen=str.length();
				res=str;
			}
		}else if(c[i]=='['){
			for(int j=i+1;j<=end;j++){
				if(c[j]==']'){
					str="["+check(c,i+1,j-1)+"]"+check(c,j+1,end);
					if(str.length()<minlen){
						minlen=str.length();
						res=str;
					}
				}
			}
			str="[]"+check(c,i+1,end);
			if(str.length()<minlen){
				minlen=str.length();
				res=str;
			}
		}else if(c[i]==']'){
			str="[]"+check(c,i+1,end);
			if(str.length()<minlen){
				minlen=str.length();
			res=str;}
		}
	return res;
}



void main(){
//	ifstream in("data.txt");
	cin>>s;
	string p=check(s,0,strlen(s)-1);
	cout<<p<<endl;



}

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