UVa 1626 (输出方案) Brackets sequence
正规括号序列定义为:
- 空序列是正规括号序列
- 如果S是正规括号序列,那么[S]和(S)也是正规括号序列
- 如果A和B都是正规括号序列,则AB也是正规括号序列
输入一个括号序列,添加尽量少的括号使之成为正规括号序列,并输出最优方案,多解的话输出任意一个即可。
设d(i, j)表示字符串s[i]~s[j]至少添加的括号的数量,则转移如下:
- S形如[S']或(S'),则转移到d(i+1, j-1)
- 如果S至少有两个字符,将其分为AB,转移到min{d(i, j), d(A) + d(B)}
不管是否满足第一条都要尝试第二种转移,因为[][]可能经过第一条转移到][
打印的时候重新检查一下最优决策。
//#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ;
char s[maxn];
int d[maxn][maxn], n; bool match(char a, char b)
{
return (a == '(' && b == ')') || (a == '[' && b == ']');
} void dp()
{
for (int i = ; i < n; ++i)
{
d[i+][i] = ; //对应空串
d[i][i] = ;
}
for (int i = n-; i >= ; --i)
{
for (int j = i+; j < n; ++j)
{
d[i][j] = n;
if(match(s[i], s[j]))
d[i][j] = min(d[i][j], d[i+][j-]);
for (int k = i; k < j; ++k)
d[i][j] = min(d[i][j], d[i][k] + d[k+][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 = d[i][j];
if(match(s[i], s[j]) && ans == d[i+][j-])
{
printf("%c", s[i]);
print(i+, j-);
printf("%c", s[j]);
return;
}
for (int k = i; k < j; ++k)
{
if(ans == d[i][k] + d[k+][j])
{
print(i, k);
print(k+, j);
return;
}
}
} void readline(char* s)
{
fgets(s, maxn, stdin);
} int main(void)
{
#ifdef LOCAL
freopen("1626in.txt", "r", stdin);
#endif int T;
readline(s);
sscanf(s, "%d", &T);
readline(s);
while(T--)
{
readline(s);
n = strlen(s) - ; //去掉最后的换行符
memset(d, -, sizeof(d));
dp();
print(, n-);
puts("");
if(T) puts("");
readline(s);
} return ;
}
代码君
UVa 1626 (输出方案) Brackets sequence的更多相关文章
- 【Uva 1626】Brackets sequence
[Link]: [Description] 括号序列由这样的规则生成: 1.空字符是一个括号序列; 2.在括号序列两端加上一对括号也是括号序列; 如(s),[s]; 3.两个括号序列A和B,连在一起, ...
- 1626 - Brackets sequence——[动态规划]
Let us define a regular brackets sequence in the following way: Empty sequence is a regular sequence ...
- [poj P1141] Brackets Sequence
[poj P1141] Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Special Judge Description ...
- POJ 题目1141 Brackets Sequence(区间DP记录路径)
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27793 Accepted: 788 ...
- POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29502 Accepted: 840 ...
- POJ1141 Brackets Sequence
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...
- UVA 1546 - Complete the sequence!(差分法)
UVA 1546 - Complete the sequence! 题目链接 题意:给定多项式前s项,求出后c项,要求尽量小 思路:利用差分法,对原序列求s - 1次差分,就能够发现规律,然后对于每多 ...
- ZOJ1463:Brackets Sequence(间隙DP)
Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...
随机推荐
- Apache CXF实现Web Service(5)—— GZIP使用
Apache CXF实现Web Service(5)-- GZIP使用 参考来源: CXF WebService整合Spring Apache CXF实现Web Service(1)--不借助重量级W ...
- 解决IE不支持position:fixed问题
#box { /* 非IE6浏览器使用固定元素 */ position:fixed; top:0; left:0; /* IE6改为绝对定位,并通过css表达式根据滚动位置更改top的值 */ _po ...
- 【面试题041】和为s的两个数字VS和为s的连续正数序列
[面试题041]和为s的两个数字VS和为s的连续正数序列 题目一: 输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s.如果有多对数字的和等于s,输出任意一对即可. ...
- iOS数组和字符串的转化
NSMutableArray *components = [messageStr componentsSeparatedByString:@"*"] ; 反过来为 NSStrig ...
- Android ListVIew 详解(一)
在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示.抽空把对ListView的使用做了整理,并写了个小例子,如下图. 列表的显示需要三 ...
- SQL技术内幕-4 row_number() over( partition by XX order by XX)的用法(区别于group by 和order by)
partition by关键字是分析性函数的一部分,它和聚合函数不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录,partition by用于给结果集分组,如果没有指 ...
- 我是如何学习 Linux 的
为何要学习 Linux? 这个问题可能困扰着很多 Linux 初学者和爱好者,其实我也说不上来为何要学习 Linux,可能最实在的理由就是—-Linux 相关工作岗位很多.在“见到” Linux 的第 ...
- Android核心分析之二十八Android GDI之Surface&Canvas
Surface&Canvas Canvas为在画布的意思.Android上层的作图几乎都通过Canvas实例来完成,其实Canvas更多是一种接口的包装.drawPaints ,drawPoi ...
- Oracle ->> 连续聚合
select id, grp_factor, sum (id) over( partition by grp_factor order by id rows between unbounded pre ...
- CDC ->> 在某个SQL Server中开启CDC功能
USE AdventureWorks;GO--开启某个数据库的CDC功能EXEC sys.sp_cdc_enable_db;GO--is_cdc_enabled栏位为1代表开启CDC功能了SELECT ...