链接:
题意:
最大括号匹配数
题解:
dp[i][j] = i~j最大括号匹配数
如果s[i]==s[j],dp[i][j] = dp[i+1][j-1]+2
否则 dp[i][j] = max(dp[i][k]+dp[k+1][j])(i=<k<r)
记忆化搜索:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include <string.h>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<time.h>
using namespace std;
#define MAX_N 105
#define inf 0x3f3f3f3f
#define LL long long
#define ull unsigned long long
const LL INF = 9e18;
const int mod = 1e8+7;
typedef pair<LL, LL>P;
string s;
int dp[MAX_N][MAX_N];
void init()
{
memset(dp, -1, sizeof(dp));
}
int cal(int l, int r)
{
if(l >= r)
return 0;
if(dp[l][r] != -1)
return dp[l][r];
int M = 0;
if((s[l]=='('&&s[r]==')') || (s[l]=='['&&s[r]==']'))
M = cal(l+1, r-1) + 2;
for(int i=l; i<r; i++)
M = max(M, cal(l, i)+cal(i+1, r));
dp[l][r] = M;
return dp[l][r];
}
int main()
{
while(cin>>s && s!="end") {
init();
printf("%d\n",cal(0, s.length()));
}
}