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. 安装11g 数据库

    出现问题解决: 1.首先确认下载的安装包完整性.2解压包的时候,按顺序解压,解压第一个包后,解压第二个包的时候,要把解压地址与解压第二包的地址要一样. 安装的时候,需要把两个压缩包都解压,并将目录wi ...

  2. C# 判断文件和文件夹是否存在并创建

    C# 判断文件和文件夹是否存在并创建 using System; using System.Data; using System.Configuration; using System.Collect ...

  3. Mac OS X:在标题栏上显示目录完整路径

    众所周知mac的finder是不带路径显示的,你进入某个文件夹只会显示当前文件夹的名字而已.虽然你可以在finder的菜单栏中点“显示”-“显示路径栏”把路径栏调出来,但是这样只会不必要的增加find ...

  4. Objective-C Memory Management 内存管理 2

    Objective-C Memory Management 内存管理  2  2.1 The Rules of Cocoa Memory Management 内存管理规则 (1)When you c ...

  5. ButterKnife 在父类 点击事件没反应的解决方案

    在用继承的方式实现butterKnife的封装的时候遇到问题, butterKnife就在baseActivity中绑定的,但是父类中公共控件点击事件无效.找了半天原因,原来是子类和父类定义的点击方法 ...

  6. 【译】x86程序员手册33-9.6中断任务和中断处理程序

    9.6 Interrupt Tasks and Interrupt Procedures 中断任务和中断处理程序 Just as a CALL instruction can call either ...

  7. python学习第三次

    while循环 表示当条件成立的时候就循环适用于不知道具体循环次数,但是确定在某个条件成立的情况下就循环while语法:while 条件表达式:语句块#另一种表达方式while 条件表达式:语句块1e ...

  8. 微擎we7模块和模板安装方法

    2017年06月08日 09:26:55 源码学习分享 阅读数:15643 标签: we7 更多 个人分类: 微擎we7   版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...

  9. SQL Server中 sysobjects、sysolumns、systypes

    1.sysobjects    系统对象表. 保存当前数据库的对象,如约束.默认值.日志.规则.存储过程等 在大多数情况下,对你最有用的两个列是Sysobjects.name和Sysobjects.x ...

  10. 关于idea的目录结构如何变成树状,也就是横向变纵向

    横向 竖向 方法: