POJ-1141:Brackets Sequence(括号序列)

一、问题描述

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.

    如果S是一个常规序列,那么(S)和[S]也都是常规序列

  3. If A and B are regular sequences, then AB is a regular sequence.

    如果A和B是常规序列,那么AB也是一个常规序列

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 …

,这个括号序列要包含已经给出的字符序列作为子序列。这里有一个字符串a1 a2 … an 是字符串b1 b2 … bm的子序列,

bm, if there exist such indices 1 = i1 < i2 < … < in = m, that aj = bij for all 1 = j = n.

如果存在这样的指数 1= i1 < i2 < … < in = m, 那么 对于所有的 1 = j = n ,有aj = bij

大概意思:

给出一个字符串(可以是空串),让所有的括号都能正确匹配(本串内的字符无法匹配的通过插入相应的字符来解决),求正确匹配的字符串最短的字符串

二、问题分析

读完这道题,了解到要求最短的字符串,属于最优化问题,所以首先想到可能是一个动态规划问题,然后接着分析,根据题目意思

用一个数组dp[i][j]表示字符串中从字符str[i]~str[j]的字符串中最少要插入多少个字符:如果str[i]与str[j]配对,则:dp[i][j]=dp[i+1][j-1],但是这道题求的是插入后的字符串,因而我们在定义一个数组:mark[i][j]:

(1)如果mark[i][j]的值为0,那么表示字符str[i]和str[j]是匹配的,那么我们就先输出str[i],再对str[i+1]~str[j-1]的字符做进一步处理;

(2)如果mark[i][j]的值不为0,那么他记录的就是与字符str[i]匹配的字符的位置,那么我们就从mark[i][j]这里划分开来,将两段字符串分开处理

三、问题解答

#include<cstdio>
#include<iostream>
#include<cstring>
#define N 105
using namespace std;

int dp[N][N],mark[N][N];
char str[N];

bool cmp(char a,char b)
{
	if((a=='('&&b==')')||(a=='['&&b==']'))
		return true;
	return false;
}

void display(int l,int r)
{
	if(l>r)
		return;
	if(l==r)
	{
		if(str[l-1]=='['||str[l-1]==']')
			printf("[]");
		else
			printf("()");
		return;
	}
	if(!mark[l][r])
	{
		printf("%c",str[l-1]);
		display(l+1,r-1);
		printf("%c",str[r-1]);
	}
	else
	{
		display(l,mark[l][r]);
		display(mark[l][r]+1,r);
	}
}

int main()
{
	while(gets(str)!=NULL)
	{
		int len=strlen(str);
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=len;i++)
		{
			dp[i][i]=1;
		}
		for(int len1=2;len1<=len;len1++)
		{
			for(int i=1;i+len1-1<=len;i++)
			{
				int j=len1+i-1;
				dp[i][j]=INF;
				if(cmp(str[i-1],str[j-1]))
				{
					dp[i][j]=dp[i+1][j-1];
					mark[i][j]=0;
				}
				for(int k=i;k<j;k++)
				{
					if(dp[i][j]>(dp[i][k]+dp[k+1][j]))
					{
						dp[i][j]=dp[i][k]+dp[k+1][j];
						mark[i][j]=k;
					}
				}
			}
		}
		display(1,len);
		printf("\n");
	}
	return 0;
}

四、生词

bracket n. 括号

sequence n. 有关联的一组事物, 一连串

indices n. 指数

五、参考文章

  1. poj1141Brackets Sequence(区间dp括号匹配打印路径)

  2. poj1141 Brackets Sequence (区间dp)

  3. POJ1141-Brackets Sequence(区间DP)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值