标签:ios char main 思路 匹配 space div code string
思路:
纯区间dp,进行一下匹配判断就好。
#include<cstdio> #include<iostream> #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]); }
标签:ios char main 思路 匹配 space div code string
原文地址:https://www.cnblogs.com/junk-yao-blog/p/9488286.html