正规括号序列定义为:

  • 空序列是正规括号序列
  • 如果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的更多相关文章

  1. 【Uva 1626】Brackets sequence

    [Link]: [Description] 括号序列由这样的规则生成: 1.空字符是一个括号序列; 2.在括号序列两端加上一对括号也是括号序列; 如(s),[s]; 3.两个括号序列A和B,连在一起, ...

  2. 1626 - Brackets sequence——[动态规划]

    Let us define a regular brackets sequence in the following way: Empty sequence is a regular sequence ...

  3. [poj P1141] Brackets Sequence

    [poj P1141] Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K   Special Judge Description ...

  4. POJ 题目1141 Brackets Sequence(区间DP记录路径)

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 788 ...

  5. POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29502   Accepted: 840 ...

  6. POJ1141 Brackets Sequence

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  7. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  8. UVA 1546 - Complete the sequence!(差分法)

    UVA 1546 - Complete the sequence! 题目链接 题意:给定多项式前s项,求出后c项,要求尽量小 思路:利用差分法,对原序列求s - 1次差分,就能够发现规律,然后对于每多 ...

  9. ZOJ1463:Brackets Sequence(间隙DP)

    Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...

随机推荐

  1. JQuery表单验证插件EasyValidator,超级简单易用!

    本插件的宗旨是:用户无需写一行JS验证代码,只需在要验证的表单中加入相应的验证属性即可,让验证功能易维护,可扩展,更容易上手. DEMO中已经包含了常用的正则表达式,可以直接复用,为了考虑扩展性,所以 ...

  2. asp.net @reqister指令

    @register指令通过声明将自定义 ASP.NET 服务器控件添加到页或用户控件中. 1.@register 指令有两种用法如下 <%@ Register tagprefix="t ...

  3. POJ 2092

    #include <iostream> #include <algorithm> #define MAXN 10005 using namespace std; int _m[ ...

  4. linux下修改history命令保存条数

    在linux系统下.history命令会保存多少条命令呢?曾在一本书上说,如果注销系统,那么会将所有的历史命令都定入到~/.bash_history, 但只保留1000条命令(这个是由默认的shell ...

  5. C#产生不重复的随机数并生成随机文件名

    本文转自:http://blog.ciznx.com/post/csharprandomnumberandrandomfilename.aspx 在项目中会遇到需要批量生成文件的时候,比如 asp.n ...

  6. 快速幂取模 POJ 3761 bubble sort

    题目传送门 /* 题意:求冒泡排序扫描k次能排好序的全排列个数 数学:这里有一个反序列表的概念,bj表示在j左边,但大于j的个数.不多说了,我也是看网上的解题报告. 详细解释:http://blog. ...

  7. a cold welcome

    What does 'a cold welcome' refer to? On wednesday evening,we went to the town hall. It was the last ...

  8. Linux之proc详解

    1. /proc目录    Linux内核提供了一种通过/proc文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...

  9. Java Servlet 技术简介

    Java Servlet 技术简介 Java 开发人员兼培训师 Roy Miller 将我们现有的 servlet 介绍资料修改成了这篇易于学习的实用教程.Roy 将介绍并解释 servlet 是什么 ...

  10. WordPress主题制作教程10:添加文章类型插件Custom Post Type UI

    下载 Custom Post Type UI>> 用Custom Post Type UI添加自定义文章类型对于新手来说最简单不过了,下载安装后,在插件栏启用一下,就可以开始添加文章类型了 ...