标签:des style blog http color os io strong
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 25132 | Accepted: 7083 | Special Judge |
Description
Input
Output
Sample Input
([(]
Sample Output
()[()]
#include <iostream> #include <stack> #include <cstring> #include <cstdio> #include <string> #include <algorithm> using namespace std; #define INF 0x3fffffff #define N 105 #define ms(a, val) memset(a, val, sizeof(a)) int dp[N][N], p[N][N]; string s; /* if(s[i,j] == "[]" || "()") dp[i][j] = dp[i+1][j-1] dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]) i<=k<j */ void print(int i, int j) { if (i > j) { return; } if (i == j) { if (s[i] == ‘(‘ || s[j] == ‘)‘) cout << "()"; else cout << "[]"; return; } if (p[i][j] == -1) { cout << s[i]; print(i + 1, j - 1); cout << s[j]; } else { print(i, p[i][j]); print(p[i][j] + 1, j); } } int main() { int n, t; while (getline(cin, s)) { n = s.length(); ms(dp, 0); for (int i = 0; i < n; i++) { dp[i][i] = 1; } for (int m = 1; m < n; m++) { for (int i = 0, j = m; j < n; i++, j++) { dp[i][j] = INF; p[i][j] = -1; if ((s[i] == ‘(‘ && s[j] == ‘)‘) || (s[i] == ‘[‘ && s[j] == ‘]‘)) { dp[i][j] = dp[i + 1][j - 1]; } for (int k = i; k < j; k++) { t = dp[i][k] + dp[k + 1][j]; if (t < dp[i][j]) { dp[i][j] = t; p[i][j] = k; } } } } print(0, n - 1); cout << endl; } return 0; }
POJ 1141 Brackets Sequence,布布扣,bubuko.com
标签:des style blog http color os io strong
原文地址:http://www.cnblogs.com/jecyhw/p/3897481.html