Untitled
unknown
plain_text
a year ago
1.6 kB
3
Indexable
Never
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <set> using namespace std; const short int INF = 1000; const short int MAXN = 200; string s; int dp[MAXN][MAXN]; string dp_data[MAXN][MAXN]; bool isOpening(char c) { return (c == '(' || c == '{' || c == '[') ? true : false; } short int type(char c) { if (c == '[' || c == ']') return 1; if (c == '{' || c == '}') return 2; if (c == '(' || c == ')') return 3; return -1; } bool compare(char c1, char c2) { return type(c1) == type(c2); } short int f(short int l, short int r) { if (l > r) return 0; if (dp[l][r] != -1) return dp[l][r]; short int first_rule = (isOpening(s[l]) && !isOpening(s[r]) && compare(s[l], s[r])) ? f(l + 1, r - 1) : INF; short int second_rule = INF, index = 0; for (short int k = l; k < r; k++) { short int v = f(l, k) + f(k + 1, r); if (second_rule > v) { second_rule = v; index = k; } } if (first_rule < second_rule) { dp[l][r] = first_rule; string res = s[l] + dp_data[l + 1][r - 1] + s[r]; dp_data[l][r] = res; } else { dp[l][r] = second_rule; string res = dp_data[l][index] + dp_data[index + 1][r]; dp_data[l][r] = res; } return dp[l][r]; } void init() { for (short int i = 0; i < MAXN; i++) { for (short int j = 0; j < MAXN; j++) { dp[i][j] = -1; } } for (short int i = 0; i < MAXN; i++) { dp[i][i] = 1; } } signed main() { cin >> s; init(); short int minimum = f(0, s.size() - 1); cout << dp_data[0][s.size() - 1] << endl; return 0; }