Link:http://poj.org/problem?id=1141
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 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 |
以下分析部分来自:http://www.cnblogs.com/ziyi--caolu/archive/2013/08/04/3236035.html
题意:给出一串括号,要你补上最少的括号使这一串括号都匹配........
思路:dp[i][j]表示从区间i到区间j使其所以括号匹配需要补上的最少括号数,那么当出现一个括号时,首先考虑它不与后面匹配的情况,那么需要加一个相对应的括号,让之匹配dp[i][j]=dp[i+1][j]+1;
然后再考虑,若是后面有括号可以让它匹配的情况,那么假设i<k<=j,当s[i]=='('&&s[k]==')'的时候,考虑动态转移,dp[i][j]=dp[i+1][k-1]+dp[k][j]-1
为什么这个动态方程减1呢,因为我将与之匹配的那个括号重新计算了一次,当s[k]==')'的时候,在计算dp[k][k]的时候,我的状态转移已经把这个括号自动匹配了一次,所以要减去这次匹配的........
然后就是记录路径了,开一个二维数组a[i][j],当a[i][j]==-1的时候,表示dp[i][j]这个状态是从dp[i+1][j]推导过来的,当a[i][j]>0的时候,表示dp[i][j]是从dp[i+1][a[i][j]-1]以及dp[k][j]这两个状态推导过来的,那么注意到当a[i][j]!=-1的时候,就正好表示s[i]与s[a[i][j]]匹配,说明在第i个括号这个地方只需要输出它自己本身,其他的,若是a[i][j]==-1的,都需要输出它自身以及与它自身匹配的括号.........
AC code:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<vector>
#include<string.h>
using namespace std;
char str[333];
int dp[333][333];//dp[i][j]表示区间i到j最大的括号匹配数
int a[333][333];//标记路径数组
int b[333];//打印数组
void path(int i ,int j)//回溯找最优路径
{
if(i>=j)
return ;
if(a[i][j]==-1)
path(i+1,j);
if(a[i][j]>0)
{
b[i]=1;
b[a[i][j]]=1;
path(i+1,a[i][j]-1);
path(a[i][j],j);
}
}
int main()
{
//freopen("D:\\in.txt","r",stdin);
int i,j,k,len;
//cin>>(str+1));
while(gets(str+1))
{
len=strlen(str+1);
memset(dp,0,sizeof(dp));
memset(a,-1,sizeof(a));
memset(b,0,sizeof(b));
for(i=0;i<=len;i++)
{
dp[i][i]=1;
}
for(i=len-1;i>=1;i--)//枚举区间起点
{
for(j=i+1;j<=len;j++)//枚举区间终点
{
dp[i][j]=dp[i+1][j]+1;
a[i][j]=-1;
if(str[i]=='('||str[i]=='[')
{
for(k=i+1;k<=j;k++)
{
if(str[k]==')'&&str[i]=='(')
{
if(dp[i][j]>dp[i+1][k-1]+dp[k][j]-1)
{
a[i][j]=k;
dp[i][j]=dp[i+1][k-1]+dp[k][j]-1;
}
}
if(str[k]==']'&&str[i]=='[')
{
if(dp[i][j]>dp[i+1][k-1]+dp[k][j]-1)
{
a[i][j]=k;
dp[i][j]=dp[i+1][k-1]+dp[k][j]-1;
}
}
}
}
}
}
path(1,len);//回溯找最优路径
for(i=1;i<=len;i++)
{
if(b[i]==1)
printf("%c",str[i]);
else
{
if(str[i]=='('||str[i]==')')
printf("()");
else if(str[i]=='['||str[i]==']')
printf("[]");
}
}
puts("");
}
return 0;
}