正规括号序列定义为:

  • 空序列是正规括号序列
  • 如果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. Sqli-labs less 39

    Less-39 和less-38的区别在于sql语句的不一样:SELECT * FROM users WHERE id=$id LIMIT 0,1 也就是数字型注入,我们可以构造以下的payload: ...

  2. ASP.NET MVC的处理管线

    原文:http://www.cnblogs.com/fzrain/p/3651693.html 下面开始解释各个部分: 路由模块 1.在ASP.NET MVC处理管线中的第一站就是路由模块.当请求到达 ...

  3. POJ 3696 The Luckiest number (欧拉函数,好题)

    该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...

  4. 【剑指offer】连续子数组最大和

    思路dp很清楚,就是要注意细节. int FindGreatestSumOfSubArray(vector<int> array) { ; ], tempsum = array[]; // ...

  5. 评论 “App死亡潮:400万应用僵尸超八成,周期仅10月”

    点这里 原文: App死亡潮:400万应用僵尸超八成,周期仅10月 时间 2015-04-05 22:48:19  和讯科技相似文章 (16)原文  http://tech.hexun.com/201 ...

  6. Android 检查设备是否存在 导航栏 NavigationBar

    尊重原创.尊重作者,转载请标明出处: http://blog.csdn.net/lnb333666/article/details/41821149 目前也没有可靠的方法来检查设备上是否有导航栏.可以 ...

  7. iOS多线程的初步研究(四)-- NSTimer

    理解run loop后,才能彻底理解NSTimer的实现原理,也就是说NSTimer实际上依赖run loop实现的. 先看看NSTimer的两个常用方法: + (NSTimer *)timerWit ...

  8. ***百度统计图表Echarts的php实现类,支持柱形图、线形图、饼形图

    /** * 百度数据统计图表echart的PHP实现类 * * 原作者: * @author: chenliujin <liujin.chen@qq.com> * @since 2013- ...

  9. POJ 3525 Most Distant Point from the Sea (半平面交向内推进+二分半径)

    题目链接 题意 : 给你一个多边形,问你里边能够盛的下的最大的圆的半径是多少. 思路 :先二分半径r,半平面交向内推进r.模板题 #include <stdio.h> #include & ...

  10. Integral类型的跨平台使用

    fundamental integral types or extended integral types 我们先通过下图,来了解可以跨平台使用的整数类型: 之所以我们需要以上各种明确指定宽度的int ...