[NYIST15]括号匹配(二)(区间dp)
2016-06-27 20:23
447 查看
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15
经典区间dp,首先枚举区间的大小和该区间的左边界,这时右边界也可计算出来。首先初始化一个匹配,那就是看看这两个括号是否匹配,即:
(s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']') ? dp(i,j) = dp(i+1,j-1)+2) : dp(i,j) = 0
接下来枚举i和j中间的所有点,更新dp(i,j)=max(dp(i,j), dp(i+m)+dp(m+1,j))寻找可能更优的匹配。
经典区间dp,首先枚举区间的大小和该区间的左边界,这时右边界也可计算出来。首先初始化一个匹配,那就是看看这两个括号是否匹配,即:
(s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']') ? dp(i,j) = dp(i+1,j-1)+2) : dp(i,j) = 0
接下来枚举i和j中间的所有点,更新dp(i,j)=max(dp(i,j), dp(i+m)+dp(m+1,j))寻找可能更优的匹配。
/* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏┓┃キリキリ♂ mind! ┛┗┛┗┛┃\○/ ┓┏┓┏┓┃ / ┛┗┛┗┛┃ノ) ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┃┃┃┃┃┃ ┻┻┻┻┻┻ */ #include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <climits> #include <complex> #include <cassert> #include <cstdio> #include <bitset> #include <vector> #include <deque> #include <queue> #include <stack> #include <ctime> #include <set> #include <map> #include <cmath> using namespace std; #define fr first #define sc second #define cl clear #define BUG puts("here!!!") #define W(a) while(a--) #define pb(a) push_back(a) #define Rint(a) scanf("%d", &a) #define Rll(a) scanf("%I64d", &a) #define Rs(a) scanf("%s", a) #define Cin(a) cin >> a #define FRead() freopen("in", "r", stdin) #define FWrite() freopen("out", "w", stdout) #define Rep(i, len) for(int i = 0; i < (len); i++) #define For(i, a, len) for(int i = (a); i < (len); i++) #define Cls(a) memset((a), 0, sizeof(a)) #define Clr(a, x) memset((a), (x), sizeof(a)) #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) #define lrt rt << 1 #define rrt rt << 1 | 1 #define pi 3.14159265359 #define RT return #define lowbit(x) x & (-x) #define onenum(x) __builtin_popcount(x) typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef pair<int, int> pii; typedef pair<string, int> psi; typedef pair<LL, LL> pll; typedef map<string, int> msi; typedef vector<int> vi; typedef vector<LL> vl; typedef vector<vl> vvl; typedef vector<bool> vb; const int maxn = 1100; int dp[maxn][maxn]; char s[maxn]; int n; int main() { // FRead(); int T; Rint(T); W(T) { Rs(s); n = strlen(s); Cls(dp); For(k, 2, n+1) { Rep(i, n-k+1) { dp[i][i+k-1] = 0; int j = i + k - 1; if((s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']')) { dp[i][j] = dp[i+1][j-1] + 2; } For(m, i, j) { dp[i][j] = max(dp[i][j], dp[i][m] + dp[m+1][j]); } } } printf("%d\n", n - dp[0][n-1]); } RT 0; }
相关文章推荐
- 修炼?如何快速成长
- Linux线程同步之读写锁
- Spark入门(Python版)
- 每天一个Linux命令(23)--linux 目录结构(一)
- 为什么访问数据库要通过端口?
- transformClassesWithJarMergingForDebug
- sql(mysql)
- 开发板镜像烧写
- C语言中最常用标准库函数
- Java 学习记录之二
- 单击按钮下拉菜单
- 【创业之路】:林锐博士谈创业
- SGU176 Flow construction
- 20150507结构体
- dojo中的this.own()
- for 循环嵌套for循环
- LuManager 2.0.97 设置shopex 手机版waptouch,绑定二级目录
- iOS控件之UIPageControl
- 网络基础知识(1)
- spring mvc @InitBinder