本文共 3361 字,大约阅读时间需要 11 分钟。
You are given two bracket sequences (not necessarily regular) s and t consisting only of characters ‘(’ and ‘)’. You want to construct the shortest regular bracket sequence that contains both given bracket sequences as subsequences (not necessarily contiguous).
Recall what is the regular bracket sequence: () is the regular bracket sequence; if S is the regular bracket sequence, then (S) is a regular bracket sequence; if S and T regular bracket sequences, then ST (concatenation of S and T) is a regular bracket sequence. Recall that the subsequence of the string s is such string t that can be obtained from s by removing some (possibly, zero) amount of characters. For example, “coder”, “force”, “cf” and “cores” are subsequences of “codeforces”, but “fed” and “z” are not.
Input
The first line of the input contains one bracket sequence s consisting of no more than 200 characters ‘(’ and ‘)’.
The second line of the input contains one bracket sequence t consisting of no more than 200 characters ‘(’ and ‘)’.
Output
Print one line — the shortest regular bracket sequence that contains both given bracket sequences as subsequences (not necessarily contiguous). If there are several answers, you can print any.
Examples
inputCopy
(())(() ()))() outputCopy (())()() inputCopy ) (( outputCopy (()) inputCopy ) ))) outputCopy ((())) inputCopy ()) (()(()(()( outputCopy (()()()(()()))
给你一个只包含左括号和右括号的序列s和t ,要求构造一个括号序列,使得所有括号匹配且s和t都是这个序列的子序列。
f[i][j][k] //a串匹配到了第i个位置,b串匹配到了第j个位置,答案序列中左括号比右括号多k个(为了保证答案序列合法,只能是左括号比右括号多)时得到的答案序列的长度
a[i]=='(' , b[j] =='(' //此时只放入一个'('即可,即 f[i][j][k]=f[i-1][j-1][k-1]+1
但为了保证答案序列的合法性,还需放入一个')',即 f[i][j][k]=f[i][j][k+1]+1
2)a[i]==')' , b[j] =='(' //需要放入一个最括号和右括号,这样正好能维持平衡,所以不需要再多见括号了。f[i][j][k]=f[i-1][j][k+1]+1,f[i][j][k]=f[i][j-1][k-1]+1
3)a[i]=='(' , b[j] ==')' //需要放入一个最括号和右括号,这样正好能维持平衡,所以不需要再多见括号了。f[i][j][k]=f[i-1][j][k-1]+1,f[i][j][k]=f[i][j-1][k+1]+1
4)a[i]==')' , b[j] ==')' //此时只放入一个')'即可,即 f[i][j][k]=f[i-1][j-1][k+1]+1
但为了保证答案序列的合法性,还需放入一个'(',即 f[i][j][k]=f[i][j][k-1]+1
最后答案为f[n][m][0]。
#include#include #include #include #include #include #include
转载地址:http://cxtn.baihongyu.com/