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

将中间过程保存在string类型的数组里面

Posted by zhb_msqx at 2007-08-20 09:27:24 on Problem 1141
In Reply To:怎样才能不超时?? 谢谢! Posted by:zhb_msqx at 2007-08-18 16:51:16
我开始也是TLE后来自习看看书发现错误了。

> #include <iostream>
> #include <fstream>
> #include <string>
> using namespace std;
> 定义一个 string tmpstr[100][100];初始化为""
> char s[100];
> 
> string check(const char *c,int start,int end){
> 	if(start>end)return "";
   这里判断tmpstr[start][end]是否等于""如果不为""直接返回,避免重复计算
> 	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;}
> 		}
         这里将res保存在tmpstr里面 tmpstr[start][end]=res;
> 	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