zoukankan      html  css  js  c++  java
  • poj 1141 Brackets Sequence ( 区间dp+输出方案 )

    http://blog.csdn.net/cc_again/article/details/10169643

    http://blog.csdn.net/lijiecsu/article/details/7589877

    如果有空串要用gets,scanf不能处理空串

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <cctype>
    #include <vector>
    #include <iterator>
    #include <set>
    #include <map>
    #include <sstream>
    using namespace std;
    
    #define mem(a,b) memset(a,b,sizeof(a))
    #define pf printf
    #define sf scanf
    #define spf sprintf
    #define pb push_back
    #define debug printf("!
    ")
    #define INF 10000
    #define MAX(a,b) a>b?a:b
    #define blank pf("
    ")
    #define LL long long
    #define ALL(x) x.begin(),x.end()
    #define INS(x) inserter(x,x.begin())
    #define pqueue priority_queue
    
    const int MAXN = 1000 + 5;
    
    int n,m;
    
    int dp[110][110],path[110][110];
    char a[110];
    
    bool match(int i,int j)
    {
        return (a[i]=='(' && a[j]== ')') || (a[i]=='[' && a[j]== ']');
    }
    void print(int i,int j)
    {
        if(i>j) return;
        if(i == j)
        {
            if(a[i] == '(' || a[i]== ')')
                pf("()");
            else
                pf("[]");
            return;
        }
        if(path[i][j] == -1)
        {
            pf("%c",a[i]);
            print(i+1,j-1);
            pf("%c",a[j]);
            return;
        }
        else
        {
            print(i,path[i][j]);
            print(path[i][j]+1,j);
        }
    }
    
    int main()
    {
        int i,j;
        while(gets(a))
        {
            n = strlen(a);
            mem(dp,0);
            mem(path,0);
            for(i=0;i<n;i++)
                dp[i][i]=1;
            for(int l = 1;l<n;l++)
            {
                for(i=0;i<n-l;i++)
                {
                    j = i+l;
                    dp[i][j] = 1<<30;
                    if(match(i,j) && dp[i+1][j-1] < dp[i][j])
                    {
                        dp[i][j] = dp[i+1][j-1];
                        path[i][j] = -1;
                    }
    
                    for(int k =i;k<j;k++)
                    {
                        if(dp[i][k]+dp[k+1][j] < dp[i][j])
                        {
                            dp[i][j] = dp[i][k]+dp[k+1][j];
                            path[i][j] = k;
                        }
                    }
                }
            }
            print(0,n-1);
            blank;
        }
        return 0;
    }
  • 相关阅读:
    idea配置svn
    idea历史版本下载
    IntelliJ IDEA 2017.1.4 x64配置说明
    IDEA 初始配置教程
    【phonegap】用本地浏览器打开网页
    【phonegap】IOS按HOME键,程序进入suspended状态,再调出,界面出现文字丢失问题
    iOS按home键后程序的状态变化
    [phonegap]安装升级
    highcharts图表显示鼠标选择的Y轴提示线
    HTML5的local storage
  • 原文地址:https://www.cnblogs.com/qlky/p/5465220.html
Copyright © 2011-2022 走看看