DP-Algo
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
const int INF = 1e9;
|
||||
bool match(char a, char b){
|
||||
return (a == '(' && b == ')') || (a == '[' && b == ']');
|
||||
}
|
||||
int main(){
|
||||
string s;
|
||||
cin >> s;
|
||||
int n = s.size();
|
||||
vector<vector<int>> dp(s.size() + 2, vector<int>(s.size() + 2, 1));
|
||||
for(int l = 2; l <= n; l++){
|
||||
for(int i = 0; i < n - l + 1; i++){
|
||||
int j = i + l - 1;
|
||||
dp[i][j] = INF;
|
||||
for(int k = i; k < j; k++){
|
||||
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
|
||||
}
|
||||
if(match(s[i], s[j])) {
|
||||
if (i + 1 <= j - 1)
|
||||
dp[i][j] = min(dp[i][j], dp[i+1][j-1]);
|
||||
else
|
||||
dp[i][j] = 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ƥ<EFBFBD><C6A5>
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << dp[0][n - 1] << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user