【POJ 1141】Brackets Sequence

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

 
以后再也不能相信POJ的SPECIAL JUDGE了,其实根本没有好伐。
搞得我跪了。。。
下了个测试数据看看。。。
咳咳。。好吧,说回这题,其实是区间DP+答案输出。
答案递归输出,也比较简单。
DP方程可以在刘汝佳老师的黑书上找到,这里不再赘述。
#include <cstdio>
#include <cstring>

using namespace std;

char s[105];
int n, f[105][105], p[105][105];

void print(int l, int r)
{
    if (l > r) return;
    if (p[l][r] == -1)
    {
        printf("%c", s[l]);
        print(l + 1, r - 1);
        printf("%c", s[r]);
        return;
    }
    if (p[l][r] == -2)
    {
        printf("%c", s[l]);
        print(l + 1, r);
        if (s[l] == '(') printf(")");
        else printf("]");
        return;
    }
    if (p[l][r] == -3)
    {
        if (s[r] == ')') printf("(");
        else printf("[");
        print(l, r - 1);
        printf("%c", s[r]);
        return;
    }
    print(l, p[l][r] - 1);
    print(p[l][r], r);
}

int main()
{
    fgets(s, 104, stdin);
    n = strlen(s) - 1;
    memset(f, 0x7f, sizeof(f));
    memset(p, 0, sizeof(p));
    for (int i = 0; i < n; ++i)
    {
        f[i][i] = 1;
        if (s[i] == '(' || s[i] == '[') p[i][i] = -2;
        else p[i][i] = -3;
        for (int j = 0; j < i; ++j)
            f[i][j] = 0;
    }
    for (int i = n - 1; i >= 0; --i)
        for (int j = i + 1; j < n; ++j)
        {
            if ((s[i] == '(' || s[j] == '[') && f[i][j] > f[i + 1][j] + 1)
            {
                f[i][j] = f[i + 1][j] + 1;
                p[i][j] = -2;
            }
            if ((s[j] == ')' || s[j] == ']') && f[i][j] > f[i][j - 1] + 1)
            {
                f[i][j] = f[i][j - 1] + 1;
                p[i][j] = -3;
            }
            if (((s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']')) && f[i][j] > f[i + 1][j - 1])
            {
                f[i][j] = f[i + 1][j - 1];
                p[i][j] = -1;
            }
            for (int k = i + 1; k <= j; ++k)
                if (f[i][j] >= f[i][k - 1] + f[k][j])
                {
                    f[i][j] = f[i][k - 1] + f[k][j];
                    p[i][j] = k;
                }
        }
    print(0, n - 1);
    printf("\n");
    return 0;
}

 

posted @ 2015-10-23 23:22  albertxwz  阅读(221)  评论(0编辑  收藏  举报