POJ1141-Brackets Sequence(区间DP)

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
()[()]

分析:

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

分析:
对于这道题,我们初始所有的字符都不能与字符串本身的字符正确匹配,这是最坏的情况。我们用一个数组dp[i][j]表示字符串中从字符str[i]~str[j]的字符串中最少要插入多少个字符:
(1)如果str[i]与str[j]配对,则:dp[i][j]=dp[i+1][j-1];
这也可能不是要插入的最少字符,例如:[][]
他的str[1]就与str[4]匹配,但是dp[2][3]的值为2,通过观察,这个字符串本身不用插入任何字符,所以我们还要通过以下操作:

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]。
(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<iostream>
#include<cstring>
#include<cstdio>
#define INF 0x3f3f3f3f
#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 Print(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]);
		Print(l+1,r-1);
		printf("%c",str[r-1]);
	}
	else
	{
		Print(l,mark[l][r]);
		Print(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;
					}
				}
			}
		}
		Print(1,len);
		printf("\n");
	}
	return 0;
}
/*([][[])*/

猜你喜欢

转载自blog.csdn.net/weixin_43357583/article/details/105879221