标签:
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 26752 | Accepted: 7553 | Special Judge |
Description
Input
Output
Sample Input
([(]
Sample Output
()[()]
Source
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; string s; int dp[110][110],pos[110][110]; void dfs(int l,int r) { if(l==r) { if(s[l]=='('||s[l]==')') cout<<"()"; else cout<<"[]"; return; } if(pos[l][r]==-1) { if(s[l]=='(') { cout<<"("; if(l+1<=r-1) dfs(l+1,r-1); cout<<")"; } else { cout<<"["; if(l+1<=r-1) dfs(l+1,r-1); cout<<"]"; } } else { dfs(l,pos[l][r]); dfs(pos[l][r]+1,r); } } int main() { getline(cin,s); if(s=="") { cout<<endl; return 0; } int n=s.length(); memset(dp,63,sizeof(dp)); for(int i=0;i<n;i++) dp[i][i]=1; memset(pos,-1,sizeof(pos)); for(int i=1;i<n;i++) for(int j=0;j+i<n;j++) { if(s[j]=='('&&s[j+i]==')'||s[j]=='['&&s[j+i]==']') { if(i==1) dp[j][j+i]=0; else dp[j][j+i]=dp[j+1][j+i-1]; } for(int k=j;k<j+i;k++) if(dp[j][j+i]>dp[j][k]+dp[k+1][j+i]) { dp[j][j+i]=dp[j][k]+dp[k+1][j+i]; pos[j][j+i]=k; } } dfs(0,n-1); cout<<endl; }
标签:
原文地址:http://blog.csdn.net/stl112514/article/details/45032927