Brackets Sequence
Description
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 a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n. Input
The input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them.
Output
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 ([(] Sample Output ()[()] Source
Northeastern Europe 2001
|
————————————————————放假的分割线————————————————————
思路:不会写。研究了一天题解。据说是lrj黑书第一道DP题。参见博客:灰机
先考虑匹配的方法。怎么匹配呢?最差的解是一个一个添加,看到左括号添一个右括号,看到右括号补一个左括号。「这个想法真心是必须有的,不要以为DP就是找最优解,DP真正的含义是:从最差的解规划出最优的解」
然后考虑“状态”及其转移。怎样表示匹配的状态?那就是起点、匹配点。d[i][i+k]。[i]表示起点,[i+k]中k表示间隔个数。白话就是:两个两个看、三个三个看、四个四个看……那么d值的意义是什么呢?既然要求“添加最少括号”的方案,那就应该DP“需要添加的括号数”。最差解我们已经有了,那就是“每个括号需要添加一个括号来匹配”。相信d[ ][ ]的初始化就写得出来了:
void init(int len)
{
For i: 0 ~ len-1
d[i][i+0] = 1;
}
然后是枚举子状态了:子状态要首先用上界赋值。然后是递推——两种情况:子状态左端和右端是匹配的;子状态左右端不匹配。
对不匹配的:DP出最优的匹配方案。怎样DP呢?要找状态转移方程了。建议在这个时候,手写一些一般性情况模拟出解、最优解。对于该状态的子状态,因为DP的原因是“最优子状态”,因此枚举它的子状态,递推出最优子结构。
例如:()(),分别命名1、2、3、4号。经过“两个两个看”,1->2、2->d[2][2]、3->4、4->d[4][4],之后是“三个三个看”,1->2 + 3->d[3][3]、2->d[2][2] + 3->4。最后是“四个四个看”,1->2 + 3->4。最优解get。
For tmp: i ~ i+k-1
if(d[i][i+k] > d[i][tmp] + d[tmp+1][i+k])
标记[i][i+k]最优分界点:tmp
d[i][i+k] = d[i][tmp] + d[tmp+1][i+k]
对匹配的:其d[ ][ ]值就等于其内部的d[ ][ ]值。这也是转移。递推。
d[i][i+k] = d[i+1][i+k-1];
此外,[i][i+k]不需要分界点,标记为-1。
到此,DP的过程完成了的说!~~接下来是输出问题。
使用递归是最好的。根据分界点来递归输出。先找递归边界:左端点 > 右端点。
情况1:左==右。因为是自己跟自己匹配,所以是“最差的解”,即:补上一个括号。
情况2:case 1:无分界点——意味着左右匹配,那么输出:左;(递归)中;右
case2:有分界点——(递归)左、分界;(递归)分界+1、右
图解:来源
代码如下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
const int N = 105;
const int SUP = 2e9;
int d[N][N], pos[N][N];
char s[N];
void match(int len)
{
for(int i = 0; i < len; i++)
d[i][i] = 1;//d值的含义:i和i+k之间应该添加的括号。起始条件:d[i][i] = 1。想一想为什么?
//思路是先两个两个看,再三个三个看……依次类推:因为靠最近且匹配的是不需要补全的。
for(int k = 1; k < len; k++)//枚举间隔
for(int i = 0; i + k < len; i++) {//枚举起点
d[i][i+k] = SUP;
if((s[i] == '(' && s[i+k] == ')') || (s[i] == '[' && s[i+k] == ']')) {//匹配
d[i][i+k] = d[i+1][i+k-1];
//情况一:左右交换:1。情况二:左右相等:1。情况三:内部需要几个它就需要几个
pos[i][i+k] = -1;//无需分开
}
//为什么不用else?看这个:[][]————外层匹配,内层不匹配。
for(int tmp = i; tmp < i + k; tmp++)//枚举分界点
if(d[i][i+k] > d[i][tmp] + d[tmp+1][i+k]) {//DP
pos[i][i+k] = tmp;//该点处分开,解更优
d[i][i+k] = d[i][tmp] + d[tmp+1][i+k];//状态转移
}
}
}
void PR(int l, int r)
{
if(l > r) return;//递归边界
if(l == r) {
if(s[l] == '(' || s[l] == ')')
printf("()");
else
printf("[]");
}
else {
if(pos[l][r] == -1) {//匹配,则递归输出内部
putchar(s[l]);
PR(l+1, r-1);
putchar(s[r]);
}
else {//不匹配,则递归至最优分界点
PR(l, pos[l][r]);
PR(pos[l][r]+1, r);
}
}
}
int main()
{
scanf("%s", s);
int len = strlen(s);
match(len);
PR(0, len-1);//递归输出解
putchar('\n');
return 0;
}