区间dp括号匹配问题 POJ2955
//#include<bits/stdc++.h>
//#include <unordered_map>
//#include<unordered_set>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<climits>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
const int INF = 0x3f3f3f3f;
const int ONF = -0x3f3f3f3f;
const int O = 1e5;
const int mod = 1e9+7;
const int maxn = 1e2+5;
const int N = 1e9+5;
const double PI = 3.141592653589;
const double E = 2.718281828459;
int main()
{
char s[maxn];
while(scanf("%s",s)&&strcmp(s,"end"))
{
int dp[maxn][maxn]; MT(dp,0);
int n = strlen(s);
for(int len= 2; len<= n; len++)
{
for(int i=0;i<n;i++)
{
int j = i + len - 1;
if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
dp[len][i] = dp[len-2][i+1] + 2;
for(int k=1;k<len;k++)
dp[len][i] = max(dp[len][i], dp[k][i]+dp[len-k][k+i]);
}
}
printf("%d\n",dp[n][0]);
}
return 0;
}