POJ 1141 Brackets Sequence 添加括号进行匹配

来源:互联网 发布:淘宝怎样找在线客服 编辑:程序博客网 时间:2024/05/29 12:21


括号匹配类问题,添加最少的括号,使得给出的括号序列匹配,输出匹配后的括号序列。

special judge


POJ的题目真奇怪,有的题不需要多组输入,这个时候用!=EOF会得到wrong answer,所以要好好读题。


Brackets Sequence
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 28749 Accepted: 8170 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


根据括号的合法定义,对于一个区间[st,ed],

定义dp[st][ed]为该区间最少要添加的括号数。

初始化dp[st][ed]=INF;

1.如果s[st]和s[ed]匹配,那么dp[st][ed]=min(dp[st][ed],dp[st+1][ed-1]);

2.

for(int k=st;k<ed;k++)

 dp[st][ed]=min(dp[st][ed],dp[st][k]+dp[k+1][ed]);


这种转移方式是根据题目说明的合法方式,还有实例研究  归纳总结出的。



/**========================================== *   This is a solution for ACM/ICPC problem * *   @source: *   @type: *   @author: wust_ysk *   @blog:  http://blog.csdn.net/yskyskyer123 *   @email: 2530094312@qq.com *===========================================*/#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>using namespace std;typedef long long ll;const int INF =0x3f3f3f3f;const int maxn=100    ;//const int maxV=12    ;char s[maxn+5];int n,dp[maxn+5][maxn+5];bool match(char a,char b){    return a=='('&&b==')'||a=='['&&b==']';}void print(int le,int ri){    if(le>ri)  return;    if(le==ri)    {        if(s[le]=='('||s[le]==')')  printf("()");        else if(s[le]=='['||s[le]==']')  printf("[]");        return;    }    if(match(s[le],s[ri])&&dp[le+1][ri-1]==dp[le][ri])    {        putchar(s[le]);        print(le+1,ri-1);        putchar(s[ri]);        return;    }    for(int k=le;k<ri;k++)    {        if(dp[le][k]+dp[k+1][ri]==dp[le][ri])        {           print(le,k);           print(k+1,ri);           return;        }    }}int main(){            scanf("%s",s+1);        n=strlen(s+1);        for(int i=1;i<=n;i++)        {            dp[i][i]=1;            dp[i+1][i]=0;        }        for(int add=1;add<n;add++)        {            for(int st=1;st+add<=n;st++)            {                int ed=st+add;                dp[st][ed]=INF;                if(match(s[st],s[ed]) )  dp[st][ed]=min(dp[st][ed],dp[st+1][ed-1] );                for(int k=st;k<ed;k++)                {                    dp[st][ed]=min(dp[st][ed],dp[st][k]+dp[k+1][ed]);                }            }        }        print(1,n);        putchar('\n');   return 0;}


0 0
原创粉丝点击