poj2955 Brackets--区间dp

原题链接:http://poj.org/problem?id=2955


题意:字符串括号最大匹配数。


#define _CRT_SECURE_NO_DEPRECATE 

#include
#include
#include
#include
#include
#include
#include
#define INF 99999999
#define eps 0.0001
using namespace std;

char s[105];
int dp[105][105];//dp[i][j]代表i,j内的最大匹配数

int main()
{
	while (~scanf("%s", s+1))
	{
		if (strcmp(s+1, "end") == 0)
			break;

		int len = strlen(s+1);
		memset(dp, 0, sizeof(dp));

		for (int l = 1; l <= len; l++)
		{
			for (int i = 1; i + l <= len; i++)
			{
				int j = i + l;
				if (s[i] == '('&&s[j] == ')' || s[i] == '['&&s[j] == ']')
					dp[i][j] = dp[i + 1][j - 1] + 2;
				for (int k = i; k <= j; k++)
					dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
			}
		}

		printf("%d\n", dp[1][len]);
	}
	return 0;
}




你可能感兴趣的:(【动态规划】--高级DP)