UVA - 1626 Brackets sequence (区间dp)
题意:给定一个串,可能空串,或由'[',']','(',')'组成。问使其平衡所需添加最少的字符数,并打印平衡后的串。
分析:dp[i][j]表示区间(i,j)最少需添加的字符数。
1、递推。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char s[MAXN];
int dp[MAXN][MAXN];
int len;
bool match(char a, char b){//判断是否平衡
return (a == '(' && b == ')') || (a == '[' && b == ']');
}
void solve(){
for(int i = 0; i < len; ++i){
dp[i + 1][i] = 0;//l>r,该情况不需添加字符
dp[i][i] = 1;//只有一个字符,无论是什么,都需要一个字符将其补全
}
for(int i = len - 2; i >= 0; --i){
for(int j = i + 1; j < len; ++j){
dp[i][j] = len;//len长度的串,最多就需要len个字符使其平衡,此处初始化成个最大值即可
if(match(s[i], s[j])){//如果当前串满足(S)或[S],则转移到S这个情况。
dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]);
}
for(int k = i; k < j; ++k){//在当前区间里枚举分割线
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
}
}
}
}
void print(int i, int j){
if(i > j) return;
if(i == j){
if(s[i] == '(' || s[i] == ')') printf("()");
else printf("[]");
return;
}
int ans = dp[i][j];
if(match(s[i], s[j]) && ans == dp[i + 1][j - 1]){
printf("%c", s[i]);
print(i + 1, j - 1);
printf("%c", s[j]);
return;
}
for(int k = i; k < j; ++k){
if(ans == dp[i][k] + dp[k + 1][j]){
print(i, k);
print(k + 1, j);
return;
}
}
}
int main(){
int T;
scanf("%d", &T);
getchar();
while(T--){
gets(s);
gets(s);
len = strlen(s);
solve();
print(0, len - 1);
printf("\n");
if(T) printf("\n");
}
return 0;
}
2、记忆化搜索,更好理解些。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char s[MAXN];
int dp[MAXN][MAXN];
int len;
bool match(char a, char b){
return (a == '(' && b == ')') || (a == '[' && b == ']');
}
int solve(int i, int j){
if(dp[i][j] != INT_INF) return dp[i][j];
if(i > j) return dp[i][j] = 0;
if(i == j) return dp[i][j] = 1;
if(match(s[i], s[j])) dp[i][j] = min(dp[i][j], solve(i + 1, j - 1));
for(int k = i; k < j; ++k){//枚举分割线,串AB所需添加的最少字符数可转移到串A所需添加的最少字符数+串B所需添加的最少字符数
dp[i][j] = min(dp[i][j], solve(i, k) + solve(k + 1, j));
}
return dp[i][j];
}
void print(int i, int j){
if(i > j) return;
if(i == j){
if(s[i] == '(' || s[i] == ')') printf("()");
else printf("[]");
return;
}
int ans = dp[i][j];
if(match(s[i], s[j]) && ans == dp[i + 1][j - 1]){
printf("%c", s[i]);
print(i + 1, j - 1);
printf("%c", s[j]);
return;
}
for(int k = i; k < j; ++k){
if(ans == dp[i][k] + dp[k + 1][j]){
print(i, k);
print(k + 1, j);
return;
}
}
}
int main(){
int T;
scanf("%d", &T);
getchar();
while(T--){
memset(dp, INT_INF, sizeof dp);
gets(s);
gets(s);
len = strlen(s);
solve(0, len - 1);
print(0, len - 1);
printf("\n");
if(T) printf("\n");
}
return 0;
}
UVA - 1626 Brackets sequence (区间dp)的更多相关文章
- UVA 1626 Brackets sequence 区间DP
题意:给定一个括号序列,将它变成匹配的括号序列,可能多种答案任意输出一组即可.注意:输入可能是空串. 思路:D[i][j]表示区间[i, j]至少需要匹配的括号数,转移方程D[i][j] = min( ...
- POJ 1141 Brackets Sequence(区间DP, DP打印路径)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- poj 1141 Brackets Sequence 区间dp,分块记录
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35049 Accepted: 101 ...
- UVa 1626 - Brackets sequence(区间DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 1626 Brackets sequence(括号匹配 + 区间DP)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/E 题意:添加最少的括号,让每个括号都能匹配并输出 分析:dp ...
- UVa 1626 Brackets sequence (动态规划)
题意:用最少的括号将给定的字符串匹配,输出最优解.可能有空行. 思路:dp. dp[i][j]表示将区间i,j之间的字符串匹配需要的最少括号数,那么 如果区间左边是(或[,表示可以和右边的字符串匹配, ...
- poj 1141 Brackets Sequence (区间dp)
题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...
- Ural 1183 Brackets Sequence(区间DP+记忆化搜索)
题目地址:Ural 1183 最终把这题给A了.. .拖拉了好长时间,.. 自己想还是想不出来,正好紫书上有这题. d[i][j]为输入序列从下标i到下标j最少须要加多少括号才干成为合法序列.0< ...
- poj 1141 Brackets Sequence ( 区间dp+输出方案 )
http://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...
随机推荐
- ch3 盒模型、定位
标准盒模型.怪异盒模型 外边距叠加 当两个或者争夺垂直外边距相遇时,他们将形成一个外边距,这个外边距的高度等于两个发生叠加的外边距的高度中的较大者. 当一个元素出现在另一个元素上面时,第一个元素的底外 ...
- SRSniffer抓包工具的使用
1.打开SRSniffer软件 2.按照1-->2-->3依次点击 3.点击左侧的启动监听按钮 4.打开要记录api的软件,查看效果
- 设计模式课程 设计模式精讲 7-3 建造者模式源码解析(jdk+guava+spring+mybaties)
1 源码解析 1.1 jdk解析 1.2 guava解析 1.3 spring解析 1.4 mybaties解析 1 源码解析 1.1 jdk解析 String public StringBuilde ...
- Spark 写 Hive table 非常慢【解决】
代码如下: dataFrame.createOrReplaceTempView("view_page_utm") val sql = s""" |in ...
- 7.Varnish
概述 Varnish处理HTTP请求的过程大致分为如下几个步骤: 1> Receive状态:请求处理入口状态,根据VCL规则判断该请求应该Pass或Pipe,还是进入Lookup ...
- [转]ubuntu备份与恢复
在 使用Ubuntu之前,相信很多人都有过使用Windows系统的经历.如果你备份过Windows系统,那么你一定记忆犹新:首先需要找到一个备份工 具(通常都是私有软件),然后重启电脑进入备份工具提供 ...
- SVM的使用
注意:数据结构的一致性,在高维度数据一般使用rbf核函数,使用网格搜索思想迭代求出gamma和c. 每行为一个样本,数据类型都围绕标黄代码而定义的. SVM训练如下坐标(左边一列为A类,右边为B类), ...
- SqlParameter 类
SqlParameter 类 表示 SqlCommand 的参数,也可以是它到 DataSet 列的映射.无法继承此类. 命名空间: System.Data.SqlClient 程序集: System ...
- WebGL绘制正方体
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CH8 课后习题
8.1和8.2 #include <iostream> using namespace std; istream& f(istream& in) { int v; in & ...