loj10150. 「一本通 5.1 练习 1」括号配对

思路:
  纯区间dp,进行一下匹配判断就好。

#include
#include
#include<string>
using namespace std;
const int maxn = 110;
string st;
int n;
int dp[maxn][maxn];
bool mate(char a, char b){
    return (a=='['&&b==']')||(a=='('&&b==')');
}
int main(void){
    cin >> st;
    n = st.size();
    st = " " + st;
    for(int i = 1; i<=n; ++i)
        dp[i][i] = 1;
    for(int L = 1; L < n; ++L)
        for(int i = 1; i + L <= n; ++i){
            int j = i + L;
            dp[i][j] = 0x7fffffff;
            for(int k = i; k < j; ++k)
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
            if(mate(st[i],st[j]))    dp[i][j] = min(dp[i][j], dp[i+1][j-1]);
        }
    printf("%d\n", dp[1][n]);        
}

 

转载于:https://www.cnblogs.com/junk-yao-blog/p/9488286.html

你可能感兴趣的:(loj10150. 「一本通 5.1 练习 1」括号配对)