码迷,mamicode.com
首页 > 其他好文 > 详细

poj1141Brackets Sequence

时间:2015-04-13 22:58:46      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

Brackets Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26752   Accepted: 7553   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 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

()[()]

Source

Northeastern Europe 2001

dp[i][j]:i到j达到匹配所需要加入最少的括号

dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j])

若s[i],s[j]匹配,则dp[i][j]=min(dp[i][j],dp[i+1][j-1])

注意一下,是有空串的

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
string s;
int dp[110][110],pos[110][110];
void dfs(int l,int r)
{
	if(l==r)
	{
		if(s[l]=='('||s[l]==')')
			cout<<"()";
		else
			cout<<"[]";
		return;
	}
	if(pos[l][r]==-1)
	{
		if(s[l]=='(')
		{
			cout<<"(";
			if(l+1<=r-1)
				dfs(l+1,r-1);
			cout<<")";
		}
		else
		{
			cout<<"[";
			if(l+1<=r-1)
				dfs(l+1,r-1);
			cout<<"]";
		}
	}
	else
	{
		dfs(l,pos[l][r]);
		dfs(pos[l][r]+1,r);
	}
}
int main()
{
	getline(cin,s);
	if(s=="")
	{
		cout<<endl;
		return 0;
	}
	int n=s.length();
	memset(dp,63,sizeof(dp));
	for(int i=0;i<n;i++)
		dp[i][i]=1;
	memset(pos,-1,sizeof(pos));
	for(int i=1;i<n;i++)
		for(int j=0;j+i<n;j++)
		{
			if(s[j]=='('&&s[j+i]==')'||s[j]=='['&&s[j+i]==']')
			{
				if(i==1)
					dp[j][j+i]=0;
				else
					dp[j][j+i]=dp[j+1][j+i-1];
			}
			for(int k=j;k<j+i;k++)
				if(dp[j][j+i]>dp[j][k]+dp[k+1][j+i])
				{
					dp[j][j+i]=dp[j][k]+dp[k+1][j+i];
					pos[j][j+i]=k;
				}
		}
	dfs(0,n-1);
	cout<<endl;
}




poj1141Brackets Sequence

标签:

原文地址:http://blog.csdn.net/stl112514/article/details/45032927

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!