zoukankan      html  css  js  c++  java
  • Brackets POJ

    题目链接
    经典括号匹配,依旧是区间的状态有影响
    若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] << "
    ";
        }
    }
    
    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;
    }
    
  • 相关阅读:
    Sightseeing,题解
    A Simple Problem,题解
    城池攻占,题解
    传递,题解
    How many ways??,题解
    Least Cost Bracket Sequence,题解
    Evacuation,题解
    Tallest Cow,题解
    容易题,题解
    无题Ⅱ,题解
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12356457.html
Copyright © 2011-2022 走看看