#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
string s;
int n;
const int size = 100;
int dp[size][size];
bool match(int i,int j){
return s[i]=='['&&s[j]==']'||s[i]=='('&&s[j]==')';
}
int MAX(int x,int y){
return x>y?x:y;
}
// two cases;
// case1: dp[i][j] = dp[i+1][j-1]+1 if (match(i, j))
// case1: dp[i][j] = max(dp[i][j], dp[i][k]+dp[k+1][j]) where k = i,...,j-1
int main(){
while (cin>>s&&s.compare("end")){
n = s.size();
memset(dp, 0, sizeof(dp));
for (int len = 2; len <= n; ++len) {
for (int i = 0; i <= n-len; ++i) {
int j = i+len-1;
if (match(i, j))
dp[i][j] = MAX(dp[i][j],dp[i+1][j-1]+2);
for (int k = 0; k <= j-1; ++k) {
dp[i][j] = MAX(dp[i][j], dp[i][k]+dp[k+1][j]);
}
}
}
cout<<dp[0][n-1]<<endl;
}
}
poj 2955
最新推荐文章于 2020-12-20 21:34:20 发布