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.
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
()[()]
此题是POJ2955的升级版,如果还未做过2955和区间dp, 请先做一下POJ2955. 此题要求还原括号的位置 个人认为区间dp可以理解为搜索, 而逆过程可以用dfs还原, 于是对每次更
新记录一下中间指针的位置. 最后dfs输出.
代码如下:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<iomanip>
#include<stdlib.h>
#include<cstdio>
#include<string>
#include<string.h>
#include<set>
#include<stack>
#include<map>
using namespace std;
#define rep(i, n) for(int i=0; i<n ;i++)
#define rep1(i, n) for(int i=1; i<=n; i++)
typedef long long ll;
typedef pair<int,int> P;
const int INF = 0x7fffffff;
const int MAX_N = 1005;
const int MAX_V = 0;
const int MAX_M = 0;
const int MAX_Q = 1e5+5;
int mark[MAX_N];
int dp[MAX_N][MAX_N];
int pos[MAX_N][MAX_N];
int N, t;
string a;
bool match(int i, int j){
if( a[i]=='('&&a[j]==')' || a[i]=='['&&a[j]==']')
return true;
return false;
}
void print(int i, int j){
//mustn't omit
if(i>j) return;
if(i==j){
if(a[i]=='('||a[i]==')') cout<<"()";
else cout<<"[]";
}
else if(pos[i][j]==-1){
cout<<a[i];
print(i+1, j-1);
cout<<a[j];
}
else{
print(i, pos[i][j]);
print(pos[i][j]+1, j);
}
}
int main(){
ios::sync_with_stdio(false);
while(getline(cin,a)){
memset(dp, 0, sizeof dp);
N = a.length();
rep(i, N) dp[i][i] = 1;
for(int j=1; j<N; j++)
for(int i=j-1; i>=0; i--){
if(match(i,j))
dp[i][j] = dp[i+1][j-1], pos[i][j] = -1;
else
dp[i][j] = INF;
for(int k=i; k<j; k++)
if(dp[i][j]>(t = dp[i][k]+dp[k+1][j]))
dp[i][j] = t, pos[i][j] = k;
}
print(0, N-1);
cout<<endl;
}
}