problem uva1626-brackets SequenceTime limit:4500 MSec problem Description
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 was followed by a blank line, and there was also a blank line between the consecutive inputs. The input file contains at most of brackets (characters ' (', ') ', ' [' and '] ') that is situated on a single line without Any other characters among them.
Outputfor Each test case, the output must follow the description below. The outputs of the consecutive cases would be separated to a blank line. Write to the output file A contains some regular brackets sequence this has the minimal possible length a nd contains the given sequence as a subsequence. Sample INPUT1
([(] Sample Output
()[()]
Solution: The problem is very good, the interval DP, can be written as a memory search, easy to ignore the place is if the brackets on both sides of the matching, then you can use the middle part of the transfer, and then the ordinary divided into the left and right intervals to transfer, the problem is more valuable in the printing process, should learn, is based on the results, the reverse push back, this convenience in no intermediate record transfer path, the price is the time there will be extra overhead, but generally not so the tle, because the solution is generally very small. The input and output have pits, need to use fgets, and note that Fgets will read the last ' \ n ', so the length of the real string needs-1.
1#include <bits/stdc++.h>2 3 using namespacestd;4 5 Const intMAXN = -+Ten;6 7 inticase, DP[MAXN][MAXN];8 CharBRA[MAXN];9 BOOLVIS[MAXN][MAXN];Ten One BOOLMatchCharACharb) { A if(A = ='('&& b = =')') || (A = ='['&& b = =']'))return true; - return false; - } the - intDP (intLintr) { - if(Dp[l][r] >=0)returnDp[l][r]; - if(L = = r)returnDP[L][R] =1; + if(L > R)returnDP[L][R] =0; - + int&ans =Dp[l][r]; AAns = r-l +1; at if(Match (Bra[l], bra[r])) { -ans = min (ans, DP (l +1, R-1)); - } - for(intK = l; K < R; k++) { -ans = min (ans, DP (l, K) + DP (k +1, R)); - } in returnans; - } to + voidAns_print (intLintr) { - if(L > R)return; the if(L = =r) { * if(Bra[l] = ='('|| BRA[L] = =')') { $printf"()");Panax Notoginseng } - Else { theprintf"[]"); + } A return; the } + - int&ans =Dp[l][r]; $ if(Match (Bra[l], bra[r]) && ans = = dp[l +1][r-1]) { $printf"%c", Bra[l]); -Ans_print (L +1, R-1); -printf"%c", Bra[r]); the return; - }Wuyi Else { the for(intK = l; K < R; k++) { - if(ans = = Dp[l][k] + dp[k +1][r]) { Wu Ans_print (l, k); -Ans_print (k +1, R); About return; $ } - } - } - } A + intMain () the { - //freopen ("Input.txt", "R", stdin); $scanf"%d\n", &icase); the while(icase--) { the fgets (Bra, MAXN, stdin); theMemset (DP,-1,sizeof(DP)); the intLen =strlen (BRA); - intAns = DP (0, Len-2); inAns_print (0, Len-2); theprintf"\ n"); the if(icase) printf ("\ n"); About fgets (Bra, MAXN, stdin); the } the return 0; the}
Uva1626-brackets sequence (Dynamic planning Basics)