题意  给定一个长度为偶数的字符串。这个字符串由三种括号组成。

   现在要把这个字符串修改为一个符合括号完全匹配的字符串,改变一个括号的代价为$1$,求最小总代价。

区间DP。令$dp[i][j]$为把子序列$[i,j]$修改为符合要求的括号序列。

其中$cnt$为调整当前最外层的那对括号所需的最小代价。

那么有状态转移方程$dp[i][j] = min(dp[i+1][j-1] + cnt, min(dp[i][k] + dp[k+1][j]))$

用记忆化搜索实现。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 53; int f[N][N];
int a[N];
int n; int dp(int l, int r){
if (l > r) return 0;
if (~f[l][r]) return f[l][r]; int cnt = 0;
if (a[l] > 0 && a[r] < 0 && a[l] + a[r] == 0) cnt = 0;
else if (a[l] > 0 || a[r] < 0) cnt = 1;
else cnt = 2; int ret = dp(l + 1, r - 1) + cnt; for (int i = l + 1; i <= r - 1; i += 2) ret = min(ret, dp(l, i) + dp(i + 1, r));
return f[l][r] = ret;
} class CorrectingParenthesization {
public:
int getMinErrors(string s){
memset(f, -1, sizeof f);
n = s.size();
rep(i, 0, n - 1){
if (s[i] == '(') a[i + 1] = 1;
else if (s[i] == '[') a[i + 1] = 2;
else if (s[i] == '{') a[i + 1] = 3;
else if (s[i] == ')') a[i + 1] = -1;
else if (s[i] == ']') a[i + 1] = -2;
else if (s[i] == '}') a[i + 1] = -3;
} return dp(1, n);
}
};

TopCoder SRM 301 Div2 Problem 1000 CorrectingParenthesization(区间DP)的更多相关文章

  1. TopCoder SRM 660 Div2 Problem 1000 Powerit (积性函数)

    令$f(x) = x^{2^{k}-1}$,我们可以在$O(k)$的时间内求出$f(x)$. 如果对$1$到$n$都跑一遍这个求解过程,时间复杂度$O(kn)$,在规定时间内无法通过. 所以需要优化. ...

  2. TopCoder SRM 701 Div2 Problem 900 ThueMorseGame(博弈+预处理)

    题意  Alice和Bob在玩一个游戏,Alice先手. 每次一个人可以从一堆式子中拿走任意数量(不超过m)的式子. 取走最后一颗式子的人胜利. 当一个取完某一步的时候剩下的石子数量的二进制表示中1的 ...

  3. Topcoder Srm 673 Div2 1000 BearPermutations2

    \(>Topcoder \space Srm \space 673 \space Div2 \space 1000 \space BearPermutations2<\) 题目大意 : 对 ...

  4. Topcoder Srm 671 Div2 1000 BearDestroysDiv2

    \(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...

  5. Codeforces Gym 100002 Problem F "Folding" 区间DP

    Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...

  6. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  7. Topcoder srm 632 div2

    脑洞太大,简单东西就是想复杂,活该一直DIV2; A:水,基本判断A[I]<=A[I-1],ANS++; B:不知道别人怎么做的,我的是100*N*N;没办法想的太多了,忘记是连续的数列 我们枚 ...

  8. topcoder SRM 628 DIV2 BracketExpressions

    先用dfs搜索所有的情况,然后判断每种情况是不是括号匹配 #include <vector> #include <string> #include <list> # ...

  9. topcoder SRM 628 DIV2 BishopMove

    题目比较简单. 注意看测试用例2,给的提示 Please note that this is the largest possible return value: whenever there is ...

随机推荐

  1. [BZOJ2947]促销(Splay)

    Description Great Bytelandish的超级市场网络请你编写一个程序模拟促销商品的成本费用(simulating costs of the promotionbeing prepa ...

  2. Hi3518EV300编译U-Boot和内核报错:loadlocale.c:130: _nl_intern_locale_data: Assertion `cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' failed. Aborted (core dumped)

    下载Hi3518EV300的SDK后编译内核和U-boot,发现爆出如下错误: scripts/kconfig/conf --silentoldconfig Kconfig Aborted (core ...

  3. (HTML)A标签伪元素选择器的继承关系

    ①如果a:link{}也存在,那么不管a{}放到哪里,a{}和a:link{}冲突的属性都会采用a:link{}的,不冲突的属性若存在a{}中,会被a:link{}. a:visited{} .a:h ...

  4. vrpie下实现vrp模型和javascript的交互

    最近在做一个vrpie的项目,用vrp建模生成vrpie,然后在网页上面显示,这里需要和网页上面的其他内容交互,现在总结一下开发经验. 第一个需求是在网页上面点击那个的时候做一些事情,通过查找sdk找 ...

  5. Java之基于Apache jar包的FTPClient上传

    首先,准备工作: http://pan.baidu.com/s/1dD1Utwt 从以上链接下载Apache的jar包,并将其复制到工程的WEB-INF下的lib包里,在此,准备工作就已经完成了. 具 ...

  6. 56、使用android studio(v1.3.*)修改包名 (rename package name)

    一.修改包名 ①选中目录,开始构造 在弹窗中选中Rename directory 在弹窗中选中Rename package 填写新的包名,点击Refactor 如果有警告,不用管它,直接点击Do Re ...

  7. 设计模式学习笔记——java中常用的设计模式

    单例设计模式(Singleton Pattern) 观察者模式(Observer Pattern) 工厂模式(Factory Pattern) 策略模式(Strategy Pattern) 适配器模式 ...

  8. 图说不为人知的IT传奇故事-2-IBM咬不动的胡桃

    此系列文章为“图说不为人知的IT传奇故事”,各位大忙人可以在一分钟甚至几秒内了解把握整个内容,真可谓“大忙人的福利”呀!!希望各位IT界的朋友在钻研技术的同时,也能在文学.历史上有所把握.了解这些故事 ...

  9. 想进BAT?这些面试题助你一臂之力

    1 软性热身题 这种题目,考的就是你的软性能力,比如表达能力,理解能力,协调能力,一个词概括就是套路.这类题目会在面试开始热身的时候,问一道两题,不会多,但是如果你能回答的有条不紊,清晰达意,那么就会 ...

  10. python-day3-内置函数与字符字节之间的转换

    #三元运算 1 if True else 0 >>>1 1 if False else 0 >>>0 #内置函数lambda def f1(a1): return ...