poj2955 Brackets
题意:就是数有一个字符串中有多少括号匹配:① ()算两种,② [ ] 算两种
题解: 和 LightOj 那道题一样
F[ i ][ j ] = max(F[ i + 1][ j ], F[ i + 1][ k - 1] + F[ k ][ j ] + 2) {i + 1 <= k <= j};
CODE:
/* Author: JDD PROG: poj2955 Brackets DATE: 2015.10.8 */ #include <cstdio> #include <cstring> #define REP(i, s, n) for(int i = s; i <= n; i ++) #define REP_(i, s, n) for(int i = n; i >= s; i --) #define MAX_N 105 using namespace std; char s[MAX_N]; int n, a[MAX_N], F[MAX_N][MAX_N]; #define max(a, b) (a > b ? a : b) void doit() { memset(F, 0, sizeof(MAX_N)); REP_(i, 1, n - 1) REP(j, i + 1, n){ F[i][j] = F[i + 1][j]; REP(k, i + 1, j) if((s[i] == '(' && s[k] == ')') || (s[i] == '[' && s[k] == ']')) F[i][j] = max(F[i][j], F[i + 1][k - 1] + F[k][j] + 2); } printf("%d\n", F[1][n]); } void init() { while(scanf("%s", s + 1) != EOF){ n = strlen(s + 1); if(n == 3 && s[1] == 'e' && s[2] == 'n' && s[3] == 'd') break; doit(); } } int main() { init(); return 0; }