【算法竞赛入门经典】递归结构的动态规划 例题9-10 UVa1626
例题UVa1626
Let us define a regular brackets sequence in the following way:
1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (S) and [S] are both regular sequences.
3. If A and B are regular sequences, then AB is a regular sequence.
For example, all of the following sequences of characters are regular brackets sequences:
(), [], (()), ([]), ()[], ()[()]
And all of the following character sequences are not:
(, [, ), )(, ([)], ([]
Some sequence of characters ‘(’, ‘)’, ‘[’, and ‘]’ is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1a2 …an is called a subsequence of the string b1b2 …bm, if there exist such indices 1 ≤ i1 < i2 < … < in ≤ m, that aj = bij for all 1 ≤ j ≤ n.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The input file contains at most 100 brackets (characters ‘(’, ‘)’, ‘[’ and ‘]’) that are situated on a single line without any other characters among them.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.
Sample Input
1
([(]
Sample Output
()[()]
分析
可以发现,一个串s可以有两种办法分解成子问题
1.如果串的最左边和最右边的字符匹配(外层匹配),例如s=[s’],那么d[i]j[]=d[i+1][j-1]
2.如果这个串有至少两个字符,那么可以拆分成s=A+B,那么d(s)=min(d(A)+d(B))
*这种情况包括不能配对的情况,例如 ‘[S’ 等等
注意:就算满足条件1也需要执行条件2来取最小的,例如[][],如果只执行第一种转换那么就造成了错误
而本题需要打印解,那么可以在打印的时候模拟求解过程,判断d[i][j]是否符合一定的条件,再进行操作。
最后,鉴于输入串可能是空串,建议使用getline()
样例实现代码
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define maxn 100+5
#define INF 10000
using namespace std;
string s;
int n, d[maxn][maxn];
bool match(char i, char j) {
return ((i == '('&&j == ')') || (i == '['&&j == ']'));
}
void dp() {
for (int i = 0; i<n; i++) {
d[i + 1][i] = 0;
d[i][i] = 1;
}
int len;
for (len = 1; len <= n; len++) {
for (int i = 0; i < n - len; i++) {
int j = i + len;
d[i][j] = INF;
if (match(s[i], s[j])) {
d[i][j] = d[i + 1][j - 1];//min?
}
for (int k = i; k < j; k++) {
d[i][j] = min(d[i][j], d[i][k] + d[k + 1][j]);
}
}
}
}
void print(int i, int j) {
if (i > j)
return;
if (i == j) {
if (s[i] == '(' || s[i] == ')')
cout << "()";
else
cout << "[]";
return;
}
int ans = d[i][j];
if (match(s[i], s[j]) && ans == d[i + 1][j - 1]) {//ans==?
cout << s[i];
print(i + 1, j - 1);
cout << s[j];
return;
}
else {
for (int k = i; k < j; k++) {
if (ans == d[i][k] + d[k + 1][j]) {
print(i, k);
print(k + 1, j);
return;
}
}
}
}
int main() {
int T;
cin >> T;
getchar();
while (T--) {
getline(cin, s);
getline(cin, s);
n = s.size();
dp();
// cout << d[0][n - 1] << endl;
print(0, n - 1);
cout << endl;
if(T)
cout<<endl;
}
return 0;
}