文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>1033.Brackets sequence

1033.Brackets sequence

时间:2010-11-10  来源:gzzcracker

#include <cstdio>
#include <cstdlib>
#include <iostream>
#define inf 0x3fffffff
using namespace std;

int f[101][101];
int p[101][101];
char s[102];

void dyna(int n) {
    int i, j, k, l, t;
    for (i = 1; i <= n; i++)
        f[i][i] = 1;
    for (l = 2; l <= n; l++) {
        for (i = 1; i <= n - l + 1; i++) {
            j = i + l - 1;
            f[i][j] = inf;
            p[i][j] = -1;
            if (s[i] == '(' && s[j] == ')' || s[i] == '[' && s[j] == ']')
                f[i][j] = f[i + 1][j - 1];
            for (k = i; k < j; k++) {
                t = f[i][k] + f[k + 1][j];
                if (t < f[i][j]) {
                    f[i][j] = t;
                    p[i][j] = k;
                }
            }
        }
    }
}

void recur(int beg, int end) {
    if (beg > end)
        return;
    if (beg == end) {
        if (s[beg] == '(' || s[beg] == ')')
            printf("()");
        else
            printf("[]");
    } else {
        if (p[beg][end] == -1) {
            printf("%c", s[beg]);
            recur(beg + 1, end - 1);
            printf("%c", s[end]);
        } else {
            recur(beg, p[beg][end]);
            recur(p[beg][end] + 1, end);
        }
    }
}

int main(int argc, char* argv[]) {
    int n;

    scanf("%s", s + 1);
    n = strlen(s + 1);

    dyna(n);
    recur(1, n);
    printf("\n");

    return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载