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 |
怎样才能不超时?? 谢谢!#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator