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 ...
随机推荐
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)D(树状数组)
//树状数组中数组的特性,有更巧妙的方法.//我们知道在树状数组中,对于数组tree[i],它所维护的区间为[i−lowbit(i)+1,i]//所以对于tree[2^i],它所维护的区间就为[1,2 ...
- 学习不一样的vue4:mock与axios实战1
学习不一样的vue4:mock与axios实战1 发表于 2017-06-14 | 分类于 web前端| | 阅读次数 8180 首先 首发博客: 我的博客 项目源码: 源码(喜欢请star) ...
- Linux centos7 linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍、 target介绍
一.linux任务计划cron crontab -u -e -l -r 格式;分 时 日 月 周 user command 文件/var/spool/corn/username 分范围0-59,时范 ...
- linux服务器上安装jdk8的两种方法
这里介绍两种安装方式: yum安装(力荐) 从官网下载包安装 获得一台linux服务器 要在linux下安装jdk,首先你得先有一台linux服务器,虚拟机或者租一台都可以 yum安装jdk 在l ...
- Python 基础之python运算符
一.运算符 1.算数运算符 + - * / // % ** var1 = 5var2 = 8 #(1) + 加res = var1 + var2print(res) # (2) - 减res = ...
- redis之Set(无序)类型常用方法总结
redis之Set(无序)类型常用方法总结 存--sadd key member [member ...] 取--SMEMBERS key sadd key member [member ...] 向 ...
- js判断数组中是否包含某个元素
参考:http://www.runoob.com/jquery/misc-inarray.html js判断数组中是否包含某个元素 $.inArray( value, array [, fromInd ...
- js正则 - 限制用户名只能中文、字母和数字 , 不能包含特殊字符
/^[\u4E00-\u9FA5A-Za-z0-9]+$/
- string简单成员函数的实现
原文:https://blog.csdn.net/zcyzsy/article/details/52146124 #include<iostream> using namespace st ...
- Redis详解(一)——RDB
Redis详解(一)--RDB 前言 由于 Redis 是一个内存数据库,所谓内存数据库,就是将数据库中的内容保存在内存中,这与传统的MySQL,Oracle等关系型数据库直接将内容保存到硬盘中相比, ...