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 ...
随机推荐
- windows网络编程-C语言实现简单的UDP协议聊天
与TCP协议下编写服务端程序代码类似,但因为是无连接的形式,所以不需要监听. 这次,我用了一点不同的想法:我建立一个服务端,用了两个端口和两个套接字,把服务端作为一个数据转发的中转站,使得客户机之间进 ...
- python 基础之列表的操作和列表的相关函数
一.列表的相关操作 1.列表的拼接 list1 = [1,2]list2 = [3,4]listvar = list1 + list2print(listvar) 2.列表的重复 lst = [1,2 ...
- ubutun18 install ibus-pinyin
ref: https://www.cnblogs.com/asmer-stone/p/9069866.html Step1 $ sudo apt-get install ibus-pinyin
- 1-1SpringBoot简介
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...
- SVM的使用
注意:数据结构的一致性,在高维度数据一般使用rbf核函数,使用网格搜索思想迭代求出gamma和c. 每行为一个样本,数据类型都围绕标黄代码而定义的. SVM训练如下坐标(左边一列为A类,右边为B类), ...
- 使用Vue.js 和Chart.js制作绚丽多彩的图表
前言 深入学习 chart.js 的选项来制作漂亮的图表.交互式图表可以给你的数据可视化提供很酷的展示方式.但是大多数开箱即用的解决方案用默认的选项并不能做出很绚丽的图表. 这篇文章中,我会教你如何自 ...
- Visual Studio Code 格式化ESlint 的方法
既然要格式化ESlint,就不得不先介绍一下什么是ESlint.后面再介绍格式化的方法 1.ESlint ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:边框表格
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:制作一个超小按钮
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- java并发初探ConcurrentHashMap
java并发初探ConcurrentHashMap Doug Lea在java并发上创造了不可磨灭的功劳,ConcurrentHashMap体现这位大师的非凡能力. 1.8中ConcurrentHas ...