POJ1141 Brackets Sequence (dp动态规划,递归)

本文出自:http://blog.csdn.net/svitter

原题:http://poj.org/problem?id=1141

题意:输出添加括号最少,并且使其匹配的串。

题解: dp [ i ] [ j ] 表示添加括号的个数, pos[ i][ j ] 表示 i , j 中哪个位置分开,使得两部分分别匹配。

pos [ i ][ j ] 为-1的时候,说明i, j 括号匹配。

初始值置dp [ i ] [ i ]  = 1; 如果只有一个括号,那么匹配结果必然是差1。

首先判断括号是否匹配,如果匹配,那么dp [ i ] [ j ] = dp[ i + 1] [ j - 1] 。如此递推dp [ i ] [ j ] 的值。

然后判断dp [ i ] [ j ] = min ( dp [ i ] [ mid ] + dp [ mid + 1 ] [ j ]);

for k = 1...len

for i = 0...i + k < len

求出dp[ i ] [ i + k ]每段。先求短区间,再求长区间,用短区间来求长区间。

可以说是http://blog.csdn.net/svitter/article/details/24877159的加强变种。

//注:除了多添加一个pos其他的一模一样。但是用当初的算法显然不符合此时的条件,只得作罢。后来借鉴了别人的代码,才推出。

再一个问题就是print_str(i , j)函数。按区间输出。、

//注:开始想到pos时,觉得没法输出放弃了,后来看到这个算法才算明白。

依据分段来输出,pos如果不为-1,那么需要从pos处分开输出。

如果pos为-1,那么输出首括号,输出中间部分,输出尾括号(两者是匹配的)

如果i == j , 如果括号为( | )则输出(), 如果括号为[ | ]则输出 [] 。

注意:必须用gets函数,因为输入数据中有空格,使用scanf函数中间会出问题。

AC代码:

//============================================================================
// Name        : test.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
//============================================================================
// Name        : 动态规划.cpp
// Author      : blog.csdn.net/svitter
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
#define MAXN 256
char br[MAXN];
int dp[MAXN][MAXN], pos[MAXN][MAXN];
int len;

void print_br(int i, int j){
	if(i > j)
		return;
	if(i == j){
		if(br[i] ==‘(‘ || br[j] == ‘)‘)
			printf("()");
		else
			printf("[]");
	}
	else if(pos[i][j] == -1){
		printf("%c", br[i]);
		print_br(i+1, j-1);
		printf("%c", br[j]);
	}
	else{
		print_br(i, pos[i][j]);
		print_br(pos[i][j] + 1, j);
	}
}

bool match(int i, int j)
{
	if(br[i] == ‘(‘ && br[j] == ‘)‘)
		return true;
	if(br[i] == ‘[‘ && br[j] == ‘]‘)
		return true;
	return false;
}

int main() {
	//work pit
	int i, j, k, mid, t;

	while (gets(br) != NULL) {
		memset(dp, 0, sizeof(dp));

		len = strlen(br);
		for (i = 0; i < len; i++)
			dp[i][i] = 1;

		for (k = 1; k < len; k++) {
			for(i = 0; i + k < len; i ++){
				j = i + k;
				dp[i][j] = 0x7fffffff;
				if(match(i, j)){//如果当前位置匹配,那么pos置-1
					dp[i][j] = dp[i+1][j-1] , pos[i][j] = -1;
				}
				for(mid = i;  mid < j; mid++){
					if(dp[i][j] > (t = dp[i][mid] + dp[mid+1][j])){//如果存在更优分解,那么选择更优分解
						dp[i][j] = t, pos[i][j] = mid;
					}
				}
			}
		}
		print_br(0, len - 1);
		printf("\n");
	}

	return 0;
}

POJ1141 Brackets Sequence (dp动态规划,递归),布布扣,bubuko.com

时间: 2024-10-27 04:57:56

POJ1141 Brackets Sequence (dp动态规划,递归)的相关文章

[ACM] POJ 1141 Brackets Sequence (区间动态规划)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25087   Accepted: 7069   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

uva 1626 Brackets Sequence ?(动态规划)

状态表示方法:d[ i ][ j ]表示的是一条序列的开始和结束: 状态定义:d[ i ][ j ]表示字串s[ i~j ] 需要添加的数量. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n; char s[105]; int d[105][105]; bool match(char ch1,char ch2) { if((ch1=='['&&am

POJ1141 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

1626 - Brackets sequence——[动态规划]

Let us define a regular brackets sequence in the following way: Empty sequence is a regular sequence. If S is a regular sequence, then (S) and [S] are both regular sequences. If A and B are regular sequences, then AB is a regular sequence. For exampl

poj 1141 Brackets Sequence dp

 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 regu

URAL 1183 Brackets Sequence DP 路径输出

题意:长度小于100的字符串s只由四种字符"()[]"组成,求以该串为子串的最短的合法串.合法串递归定义为: (1)空串合法 (2)如果S合法,则(S).[S]合法 (3)如果A.B合法,则AB合法 思路: 设dp[i][j]为s(i,j)变为合法串后,合法串的长度或需要添加的字符的个数,状态转移: (1)如果s[i]和s[j]匹配,dp[i,j]=dp[i+1,j-1]. (2)如果不匹配,划分s(i,j)为s(i,k)和s(k+1,j),划分后dp[i,j]=dp[i,k]+dp[

ural Brackets Sequence (dp)

http://acm.timus.ru/problem.aspx?space=1&num=1183 很经典的问题吧,看的黑书上的讲解. 设dp[i][j]表示i到j括号合法需要的最少括号数. 共有四种情况: s[i]s[j]配对,dp[i][j] = min( dp[i][j] ,  dp[i-1][j+1] ): s[i] = '('或'[' dp[i][j] = min( dp[i][j] , dp[i+1][j]+1 ): s[j] = ')'或']' dp[i][j] = min( dp

uva1626 poj 1141 Brackets Sequence 区间dp 打印路径

// poj 1141 Brackets Sequence // 也是在紫书上看的一题,uva就是多了一个t组数据. // 经典区间dp // dp(i,j)表示区间[i,j]内所需要增加的括号数目 // 则分为两种情况 // 一种是s[i]和s[j]是匹配的则 // dp[i][j] = min(dp[i][j],dp[i+1][j-1]) // 另外一种情况是不匹配 // dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]){i<k<j}; // 但是无

POJ 1141 Brackets Sequence (区间DP)

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