uva1626 Brackets sequence

来源:互联网 发布:路由器mac地址过滤功能 编辑:程序博客网 时间:2024/06/07 07:33

Brackets sequence

Time Limit: 4500MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
[Submit] [Go Back] [Status] 

Description
Download as PDF
Let us define a regular brackets sequence in the following way:
Empty sequence is a regular sequence.
If S is a regular sequence, then (S) and [S] are both regular sequences.
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 a1a2...an is called a subsequence of the string b1b2...bm, if there exist such indices 1 ≤ i1 < i2 < ... < in ≤ m, that aj=bij for all 1 ≤ j ≤ n.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
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
1

([(]
Sample Output

()[()]

题意:给出一串括号字符,要求补上最少的括号是括号完全匹配,'('与‘)’匹配,‘[’和‘]’匹配。

DP,dp[i][j]表示i-j这段要完全匹配需要最少补上几个。。

状态转移方程是。。如果i和j匹配,dp[i][j]=min(dp[i][j],dp[i+1][j-1]).  意思就是i和j匹配直接找i到j这段和i+1到j-1这段的最小值就可以

还有一个是 dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]),把i到j分成两段,找最小。。

输出看代码。。。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int INF=1<<30;int dp[110][110];bool match(char a,char b){    if(a=='('&&b==')')        return 1;    if(a=='['&&b==']')        return 1;    return 0;}char s[110];void prin(int i,int j){    if(i>j)        return;    if(i==j)    {        if(s[i]=='('||s[i]==')')            printf("()");        else            printf("[]");        return;    }    int ans=dp[i][j];    if(match(s[i],s[j])&&ans==dp[i+1][j-1])    {        printf("%c",s[i]);        prin(i+1,j-1);        printf("%c",s[j]);        return;    }    for(int k=i;k<j;k++)    {        if(ans==dp[i][k]+dp[k+1][j])        {            prin(i,k);            prin(k+1,j);            return;        }    }}int main(){    int t,i,n,j,flag=0;    scanf("%d",&t);    getchar();    while(t--)    {        gets(s);        gets(s);        n=strlen(s);        if(n==0)        {            if(flag++)                printf("\n");            printf("\n");            continue;        }        for(i=0;i<n;i++)        {            dp[i][i]=1;        }        for(i=n-2;i>=0;i--)        {            for(j=i+1;j<n;j++)            {                dp[i][j]=INF;                if(match(s[i],s[j]))                    dp[i][j]=min(dp[i][j],dp[i+1][j-1]);                for(int k=i;k<j;k++)                {                    dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);                }            }        }        if(flag++)            printf("\n");        prin(0,n-1);        printf("\n");    }    return 0;}


0 0