博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1141 Brackets Sequence
阅读量:6111 次
发布时间:2019-06-21

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

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

()[()] 题意 给一个只有括号的字符串,请添加尽量少的括号使其变为合法的,并输出。 合法的定义如下: 1.空序列是合法的。 2.如果s是合法的,那么(s)和[s]也是合法的。 3.如果A和B都是合法的,那么AB也是合法的。 分析 设串S至少需要增加d(S)个括号,转移如下: 1.如果S形如(S`)或者[S`],转移到d(S`)。 2.如果S至少有两个字符,则可以分为AB,转移到d(A)+d(B)。 边界是:当S为空串,d(S)=0;为单字符时,d(S)=1。 注意:不管S是否满足第一种情况,都需要尝试第二种情况,否则"[][]"会转移到"]["。 由此,定义dp[i][j]为字符串第i个字符到第j个字符变成合法的至少需要添加几个括号。 另外,本题还要输出构造的合法串,于是用个二维数组记录切割的位置,最后递归输出。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long LL;const int maxn = 1e4+5;const int mod = 77200211+233;typedef pair
pii;#define X first#define Y second#define pb push_back//#define mp make_pair#define ms(a,b) memset(a,b,sizeof(a))const int inf = 0x3f3f3f3f;#define lson l,m,2*rt#define rson m+1,r,2*rt+1typedef long long ll;#define N 100010//dp[i][j]表示区间i到j需要的最少括号数int dp[105][105],pos[105][105];char s[105];bool match(char a,char b){ if((a=='('&&b==')') || (a=='['&&b==']')) return true; return false;}void Print(int i,int j){ if(i>j) return; if(i==j){ if(s[i]=='('||s[i]==')') printf("()"); else printf("[]"); return; } if(pos[i][j]==-1){ if(s[i]=='('){ printf("("); Print(i+1,j-1); printf(")"); }else{ printf("["); Print(i+1,j-1); printf("]"); } }else{ Print(i,pos[i][j]); Print(pos[i][j]+1,j); }}int main(){#ifdef LOCAL freopen("in.txt","r",stdin);#endif // LOCAL while(gets(s+1)){ int len = strlen(s+1);// cout<
<

 

 

转载于:https://www.cnblogs.com/fht-litost/p/8858178.html

你可能感兴趣的文章
纯CSS3绘制的黑色图标按钮组合
查看>>
Linux中环境变量文件及配置
查看>>
从0开始学Flutter
查看>>
mysql操作入门基础之对数据库和表的增删改查
查看>>
IIS负载均衡
查看>>
分布式事务,EventBus 解决方案:CAP【中文文档】
查看>>
Linux下的CPU性能瓶颈分析案例
查看>>
spring mvc入门
查看>>
2012在数据库技术会议上的讲话PPT打包
查看>>
【Android】 TextView设置个别字体样式
查看>>
python svn
查看>>
raise语句
查看>>
sequence2(高精度dp)
查看>>
如何向 Linux 内核上游提交 Patch ?
查看>>
Go编程笔记(7)
查看>>
Go语言int类型绑定方法
查看>>
pid控制的文章
查看>>
MySQL中EXPLAIN命令详解
查看>>
redis 单点部署
查看>>
Java中需要编码的场景
查看>>