Brackets POJ - 2955

题目链接
经典括号匹配,依旧是区间的状态有影响
若i,j匹配,dpi,j=dpi+1,j-1+2
然后最优选择
dpi,j=max(dpi,k+dpk,j)

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<int,int> pii;

const int maxn = 105;
const int INF = 0x3f3f3f3f;
int dp[maxn][maxn];
char str[maxn];

bool valid(int x, int y) {
    return (str[x] == '[' && str[y] == ']') || (str[x] == '(' && str[y] == ')');
}

void run_case() {
    while(cin >> (str+1) && strcmp(str+1, "end")) {
        int n = strlen(str+1);
        memset(dp, 0, sizeof(dp));
        for(int i = n-1; i >= 1; --i) {
            for(int j = i+1; j <= n; ++j) {
                if(valid(i, j))
                    dp[i][j] = max(dp[i][j], dp[i+1][j-1] + 2);
                for(int k = i; k <= j; ++k)
                    dp[i][j] = max(dp[i][j], dp[i][k]+dp[k][j]);
            }
        }
        cout << dp[1][n] << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cout.flags(ios::fixed);cout.precision(10);
    //int t; cin >> t;
    run_case();
    cout.flush();
    return 0;
}
posted @ 2020-02-24 12:45  GRedComeT  阅读(121)  评论(0编辑  收藏  举报