POJ 2955 Brackets 区间DP 最大括号匹配

http://blog.csdn.net/libin56842/article/details/9673239

http://www.cnblogs.com/ACMan/archive/2012/08/09/2630497.html

http://blog.csdn.net/chaiyuan414/article/details/5448699

 

#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("!\n")
#define INF 10000
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#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];
char a[110];

int main()
{
    int i,j;
    while(sf("%s",a)==1 && a[0]!='e')
    {
        n = strlen(a);
        mem(dp,0);
        for(int l = 1;l<n;l++)
        {
            for(i=0;i<n-l;i++)
            {
                j = i+l;
                if((a[i]=='(' && a[j]== ')') || (a[i]=='[' && a[j]== ']'))
                    dp[i][j] = dp[i+1][j-1]+2;

                for(int k =i+1;k<j;k++)
                {
                    dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+1][j]);
                }

            }
        }
        pf("%d\n",dp[0][n-1]);
    }
    return 0;
}

 

posted @ 2016-05-03 11:23  qlky  阅读(248)  评论(0编辑  收藏  举报