#include<iostream>
using namespace std;
char s[150];
int a[150][150],f[150][150];
int size,temp;
void print(int i,int j){
if(i>j)
return;
if(f[i][j]==-1){
if(i==j){
if(s[i]=='('||s[i]==')'){
cout<<"()";
}else if(s[i]=='['||s[i]==']'){
cout<<"[]";
}
}else{
cout<<s[i];
print(i+1,j-1);
cout<<s[j];
}
}else{
print(i,f[i][j]);
print(f[i][j]+1,j);
}
}
int main(){
while(gets(s)!=NULL){
size=-1;
while(s[++size]!=NULL);
if(size==0){
cout<<endl;
continue;
}
memset(f,-1,sizeof(f));
memset(a,0,sizeof(a));
for(int i=0;i<size;i++){
for(int j=0,k=i;k<size;j++,k++){
if(j==k){
a[j][k]=1;
}else{
temp=100000;
if(s[j]=='('&&s[k]==')'){
temp=min(temp,a[j+1][k-1]);
}else if(s[j]=='['&&s[k]==']'){
temp=min(temp,a[j+1][k-1]);
}
for(int m=j;m<k;m++){
if(temp>(a[j][m]+a[m+1][k])){
temp=a[j][m]+a[m+1][k];
f[j][k]=m;
}
}
a[j][k]=temp;
}
}
}
print(0,size-1);
cout<<endl;
}
return 0;
}
poj1141
最新推荐文章于 2019-04-06 14:50:43 发布