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. [BZOJ1878][SDOI2009]HH的项链 莫队

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1878 不带修改的莫队,用一个桶记录一下当前区间中每种颜色的数量就可以做到$O(1)$更新了 ...

  2. 浅谈网上的zoomlistview存在的问题

    最近项目主要是做一个类似wps文档阅历的功能,以列表的形式显示文档,并且需要实现缩放平移.而网上关于此类功能的实现主要是通过自定义的listview实现的,类名为ZoomListView. 网上的zo ...

  3. QPushButton注册事件过滤器后按钮消失

    版权声明:本文为博主原创文章,转载需要注明出处. RT,代码如下: ui.btn_set->installEventFilter(this); bool MousrHoverTest::even ...

  4. 生产者-消费者中的缓冲区:BlockingQueue接口

    BlockingQueue接口使用场景相信大家对生产者-消费者模式不陌生,这个经典的多线程协作模式,最简单的描述就是生产者线程往内存缓冲区中提交任务,消费者线程从内存缓冲区里获取任务执行.在生产者-消 ...

  5. laravel UserRequest $request error

    laravel UserRequest $request error Ask Question   0   laravel5.2,I create a UserRequest.php under Re ...

  6. SVG 浏览器支持

    可以参考以下链接: https://caniuse.com/#search=svg https://en.wikipedia.org/wiki/Comparison_of_layout_engines ...

  7. C#解析单层html的中的文本,然后拼接起来

    匹配单层html的小demo,应该能匹配大多数html字符串.多层(嵌套)html标签解析不出来.可能有小bug,我抛砖引玉下,哈哈. using System; using System.Colle ...

  8. ubuntu 18.04 安装.net core

    要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系那就只好在琢磨了,然后就发现了在github有安装的方法因为是18.04 所以 wget -qO- https://packages.micro ...

  9. 第3节 mapreduce高级:4、倒排索引的建立

    倒排索引建立 需求分析 需求:有大量的文本(文档.网页),需要建立搜索索引 最终实现的结果就是哪个单词在哪个文章当中出现了多少次 思路分析: 首选将文档的内容全部读取出来,加上文档的名字作为key,文 ...

  10. Android图像处理之BitMap(2)

    Bitmap 相关 1. Bitmap比较特别 因为其不可创建 而只能借助于BitmapFactory 而根据图像来源又可分以下几种情况: * png图片 如:R.drawable.tianjin J ...