Poj1141

题目描述:

定义合法的括号序列如下:

1 空序列是一个合法的序列

2 如果S是合法的序列,则(S)和[S]也是合法的序列

3 如果A和B是合法的序列,则AB也是合法的序列

例如:下面的都是合法的括号序列

(),  [],  (()),  ([]),  ()[],  ()[()]

下面的都是非法的括号序列

(,  [,  ),  )(,  ([)],  ([(]

给定一个由'(',  ')',  '[', 和 ']' 组成的序列,找出以该序列为子序列的最短合法序列。

Brackets Sequence

Description

Let us define a regular brackets sequence in the following way:
1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (S) and [S] are both regular sequences.
3. If A and B are regular sequences, then AB is a regular sequence.

For example, all of the following sequences of characters are regular brackets sequences:
(), [], (()), ([]), ()[], ()[()]
And all of the following character sequences are not:
(, [, ), )(, ([)], ([(]

Some sequence of characters '(', ')', '[', and ']' is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.

Input

The input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them.

Output

Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.

Sample Input

([(]

Sample Output

()[()]

分析:先用动态规划处理一个f[i][j]数组表示把i-j之间的括号处理成合法序列的最
小步数,处理完成之后,再用深搜把序列打出来
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define M 210
using namespace std;
char s[M];
int f[M][M];
bool check(char x,char y)
{
if(x=='('&&y==')') return true;
if(x=='['&&y==']') return true;
return false;
}
void work(int l,int r)
{
if(l>r) return ;
if(l==r)
{
if(s[l]=='('||s[r]==')') printf("()");
if(s[l]=='['||s[r]==']') printf("[]");
return ;
}
int tot=f[l][r];
if(tot==f[l+][r-]&&check(s[l],s[r]))
{
printf("%c",s[l]);
work(l+,r-);
printf("%c",s[r]);
return;
}
for(int k=l;k<r;k++)
if(tot==f[l][k]+f[k+][r])
{
work(l,k);work(k+,r);return ;
}
}
int main()
{
freopen("jh.in","r",stdin);
scanf("%s",s+);
int n=strlen(s+);
for(int w=;w<=n;w++)f[w][w]=;
for(int l=;l<n;l++)
for(int i=;i<=n-l;++i)
{
int j=l+i;
f[i][j]=;
if(check(s[i],s[j]))
f[i][j]=f[i+][j-];
for(int k=i;k<=j-;k++)
if(f[i][k]+f[k+][j]<f[i][j])
f[i][j]=f[i][k]+f[k+][j];
}
work(,n);
printf("\n");
return ;
}

括号序列(Poj1141)的更多相关文章

  1. 括号序列问题 uva 1626 poj 1141【区间dp】

    首先考虑下面的问题:Code[VS] 3657 我们用以下规则定义一个合法的括号序列: (1)空序列是合法的 (2)假如S是一个合法的序列,则 (S) 和[S]都是合法的 (3)假如A 和 B 都是合 ...

  2. BZOJ4350: 括号序列再战猪猪侠

    Description 括号序列与猪猪侠又大战了起来. 众所周知,括号序列是一个只有(和)组成的序列,我们称一个括号 序列S合法,当且仅当: 1.( )是一个合法的括号序列. 2.若A是合法的括号序列 ...

  3. DP专题——括号序列

    毕竟是个渣,写完一遍之后又按LRJ的写了一遍,再写了一遍递归版,最终加上输出解部分 括号序列 定义如下规则序列(字符串): 空序列是规则序列: 如果S是规则序列,那么(S)和[S]也是规则序列: 如果 ...

  4. 【BZOJ】2209: [Jsoi2011]括号序列(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2209 splay又犯逗........upd1那里的sum忘记赋值反............. 本题 ...

  5. 51nod1476 括号序列的最小代价

    这题应该可以用费用流写吧?不过我想不出贪心来TAT.其实还是单调队列乱搞啊T_T //ÍøÉϵÄ̰ÐÄËã·¨ºÃÉñ°¡¡£¡£¡£ÎÒÖ»»áÓÃ×îС·ÑÓÃ×î´óÁ÷ÅÜTAT #in ...

  6. lintcode: 有效的括号序列

    题目: 有效的括号序列 给定一个字符串所表示的括号序列,包含以下字符: '(', ')', '{', '}', '[' and']', 判定是否是有效的括号序列. 样例 括号必须依照 "() ...

  7. uoj #31. 【UR #2】猪猪侠再战括号序列 贪心

    #31. [UR #2]猪猪侠再战括号序列 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/31 Descript ...

  8. bzoj 1095 [ZJOI2007]Hide 捉迷藏(括号序列+线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1095 [题意] 给定一棵树,树上颜色或白或黑而且可以更改,多个询问求最远黑点之间的距离 ...

  9. CODEVS 3657 括号序列

    [问题描述] 我们用以下规则定义一个合法的括号序列: (1)空序列是合法的 (2)假如S是一个合法的序列,则 (S) 和[S]都是合法的 (3)假如A 和 B 都是合法的,那么AB和BA也是合法的 例 ...

随机推荐

  1. selenium通过autoit实现上传和下载

    autoit安装目录如下: AutoIt Windows Info   用于帮助我们识Windows控件信息. Compile Script to.exe 用于将AutoIt生成 exe 执行文件. ...

  2. 微信打开网址添加在浏览器中打开提示 http://caibaojian.com/weixin-tip.html

    原文链接:http://caibaojian.com/weixin-tip.html#t2 使用微信打开网址时,无法在微信内打开常用下载软件,手机APP等.网上流传的各种微信打开下载链接,微信已更新基 ...

  3. poj3109 Inner Vertices

    思路: 树状数组 + 扫描线. 实现: #include <cstdio> #include <vector> #include <algorithm> using ...

  4. Swiper插件轮播

    <html><head> <meta charset="utf-8"> <title>Swiper轮播</title>& ...

  5. Javascript数据结构之栈

    作者原文:http://hawkzz.com/blog/blog/1515054561771 定义 栈是一种特殊的列表,栈内的元素只能通过列表的一端访问,这一端称为栈顶.栈被称为一种先入后出的数据结构 ...

  6. 我用 Python 爬了智联“北上广深”5400条 Java 招聘数据

    结论 国际惯例,先上结论. Java 类职位招聘,不论是需求量(工作机会),还是工资平均水平,都是帝都北京最好. 北京和上海的平均工资差距不大(不超过200/月),但上海的需求量是北京的一半,机会更少 ...

  7. php5.6.30开启redis扩展

    注:5.6版本的php一定要下载phpredis3.0以上的版本,之前自己下载用的2.2.4的redis,安装配置完成后,PHP死活不支持redis的扩展,通过phpinfo打印也压根看不到,重复服务 ...

  8. System.Lazy<T>延迟加载

    在很多情况下,有些对象需要在使用时加载或根据逻辑动态加载.有些情况如果不延迟加载,可能会影响效率甚至抛出Timeout Exception.如网络操作.数据库操作.文件IO操作 直接上代码,方便我们理 ...

  9. Windows之shortcut

    System key combinations CTRL+ESC: Open Start menu ALT+TAB: Switch between open programs ALT+F4: Quit ...

  10. pdf 使用模板下载

    //根据模板下载模板 /** * * 政策5-8条的创建的pdf的模板 */public String createPdfCashTemplate(PdfCashParam pdfCashParam) ...