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