博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1626 - Brackets sequence——[动态规划]
阅读量:5217 次
发布时间:2019-06-14

本文共 2364 字,大约阅读时间需要 7 分钟。

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 a1a2...an is called a subsequence of the string b1b2...bm, if there exist such indices 1 ≤ i1 < i2 < ... < in ≤ m, that aj=bij for all 1 ≤ j ≤ n.

Input 

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

 

 

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

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

 

 

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 

1([(]

Sample Output 

()[()] 紫书上有详解+代码,就不废话了直接贴代码:
1 #include 
2 #include
3 #include
4 using namespace std; 5 const int maxn = 110; 6 char s[maxn]; 7 int d[maxn][maxn]; 8 int n; 9 inline bool match(char a,char b){10 if((a=='('&&b==')')||(a=='['&&b==']')) return true;11 else return false;12 }13 void dp(){14 for(int i=0;i
=0;i--){20 for(int j=i+1;j
j) return;32 if(i==j){33 if(s[i]=='('||s[i]==')') printf("()");34 else printf("[]");35 return;36 }37 int ans=d[i][j];38 if(match(s[i],s[j])&&ans==d[i+1][j-1]){39 printf("%c",s[i]);print(i+1,j-1);printf("%c",s[j]);40 return;41 }42 for(int k=i;k

 

转载于:https://www.cnblogs.com/Kiraa/p/5516125.html

你可能感兴趣的文章
js 对象
查看>>
centos走一波
查看>>
.Net 开源项目资源大全
查看>>
我的Python入门笔记(14)
查看>>
课本学习笔记5:第七章 20135115臧文君
查看>>
广州捷宝科技(Android+WINCE+JBOS)
查看>>
Linux课程笔记 http基础
查看>>
『TCP/IP详解——卷一:协议』读书笔记——09
查看>>
python模块整理8-glob(类似grep)和fnmatch(匹配文件名)
查看>>
PHP 实时获取文件大小
查看>>
JZOJ 1003【东莞市选2007】拦截导弹——dp
查看>>
git
查看>>
Java的初始化块、静态初始化块、构造函数的执行顺序及用途探究
查看>>
[原]Kettle连接SQL Server 2005 Express数据库
查看>>
jQuery插件开发全解析<转>
查看>>
ActiveMQ两种模式PTP和PUB/SUB<转>
查看>>
转 : React Native 开发之 IDE 选型和配置
查看>>
条件分支和循环
查看>>
使用jsp/servlet简单实现文件上传与下载
查看>>
es快照备份和跨集群恢复
查看>>