Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 22168 | Accepted: 6232 | Special Judge |
Description
Input
Output
Sample Input
([(]
Sample Output
()[()]
Source
#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> using namespace std; //#define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) const int MAXN = 100 + 50; const int maxw = 100 + 20; const int MAXNNODE = 1000000 +10; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 0x7fffffff; const int IMIN = 0x80000000; #define eps 1e-8 #define mod 1000000007 typedef long long LL; const double PI = acos(-1.0); typedef double D; typedef pair<int , int> pi; int dp[MAXN][MAXN]; int path[MAXN][MAXN]; char s[MAXN]; int n; void print(int l , int r) { if(l > r)return; if(l == r) { if(s[l] == '('||s[l] == ')')cout << "()"; else cout << "[]"; } else if(path[l][r] == -1) { cout << s[l]; print(l + 1 , r - 1); cout << s[r]; } else { print(l , path[l][r]); print(path[l][r] + 1 , r); } } int main() { //ios::sync_with_stdio(false); #ifdef Online_Judge freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif // Online_Judge while(gets(s)!=NULL) { n = strlen(s); clr(dp , 0); clr(path , 0); FORR(i , 0 , n){dp[i][i] = 1;dp[i + 1][i] = 0;} FOR(k , 1 , n)FOR(i , 0 , n - k) { int j = i + k; dp[i][j] = INF; path[i][j] = -1; if((s[i] == '('&&s[j] == ')')||(s[i] == '['&&s[j] == ']')) { dp[i][j] = min(dp[i][j] , dp[i + 1][j - 1]); } FOR(l , i , j)if(dp[i][l] + dp[l + 1][j] < dp[i][j]) { dp[i][j] = dp[i][l] + dp[l + 1][j]; path[i][j] = l; } } print(0 , n - 1); cout << endl; } return 0; }