#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <sstream>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <bitset>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define repf(i, a, b) for(int i = a; i <= b; i++)
#define PB push_back
#define MP make_pair
#define CC(a, b) memset(a, b, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define lowbit(x) ((x) & (-x))
#define foreach(it, a) for(__typeof((a).begin()) it=(a).begin(); it!=(a).end(); it++)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define x first
#define y second
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7f7f7f7f;
const int maxn = 111;
const int mod = 1000000007;
const double eps = 1e-8;
char str[maxn];
int dp[maxn][maxn];
int dfs(int l, int r) {
if(l == r) return 1;
if(l > r) return 0;
if(dp[l][r] != inf) return dp[l][r];
if(str[l] == '(' && str[r] == ')' || str[l] == '[' && str[r] == ']') {
dp[l][r] = dfs(l + 1, r - 1);
}
for(int i = l; i <= r; i++) {
dp[l][r] = min(dp[l][r], dfs(l, i) + dfs(i + 1, r));
}
return dp[l][r];
}
int main() {
int t; cin >> t;
while(t--) {
cin >> str;
CC(dp, 0x7f);
cout << dfs(0, strlen(str) - 1) << endl;
}
}
CodingTrip - 携程编程大赛 (预赛第一场)第二题 括号匹配
最新推荐文章于 2014-04-22 20:44:42 发布