POJ-1141 Brackets Sequence 区间dp

本文介绍了一种算法,用于寻找包含给定括号序列的最短合法括号序列。通过动态规划记录更新过程中中间指针的位置,并使用深度优先搜索进行结果输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

()[()]


此题是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;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值