Topic Portal
1 /*2 memory Search +dfs:dp[i][j] denotes the first and the first J characters, and the minimum number of parentheses to be added3 Dp[x][x] = 1 Be sure to add a bracket; dp[x][y] = 0, x > y;4 when s[x] matches S[y], the search (x+1, y-1); otherwise, the matching parentheses are found in the X~y-1 enumeration, and the minimum value is updated5 */6#include <cstdio>7#include <algorithm>8#include <cmath>9#include <iostream>Ten#include <cstring> One using namespacestd; A - Const intMAXN = 1e2 +Ten; - Const intINF =0x3f3f3f3f; the intDP[MAXN][MAXN]; - intPOS[MAXN][MAXN]; - CharS[MAXN]; - + voidDFS (intXinty) - { + if(Dp[x][y]! =-1)return ; A if(x > Y) {Dp[x][y] =0;return ;} at if(x = = y) {Dp[x][y] =1;return ;} - -Dp[x][y] =INF; - if((s[x]=='('&& s[y]==')') || (s[x]=='['&& s[y]==']')) - { -Pos[x][y] =-1; inDFS (x+1, Y1); -Dp[x][y] = dp[x+1][y-1]; to } + for(intI=x; i<y; ++i) - { theDFS (x, i); DFS (i+1, y); * intCur = dp[x][i] + dp[i+1][y]; $ if(Cur <Dp[x][y])Panax Notoginseng { -Dp[x][y] = cur; Pos[x][y] =i; the } + } A } the + voidPrintintXinty) - { $ if(x > Y)return ; $ if(x = =y) - { - if(S[x] = ='('|| S[y] = =')') printf ("()"); the Elseprintf ("[]"); - return ;Wuyi } the if(Pos[x][y] = =-1) - { Wuprintf ("%c", s[x]); -Print (x+1, Y1); Aboutprintf ("%c", S[y]); $ } - Else - { -Print (x, pos[x][y]); Print (pos[x][y]+1, y); A } + } the - intMainvoid)//URAL 1183 Brackets Sequence $ { the //freopen ("o.in", "R", stdin); the the while(SCANF ("%s", s+1) ==1) the { - intLen = strlen (s+1); inMemset (DP,-1,sizeof(DP)); thememset (POS,0,sizeof(POS)); the AboutDFS (1, Len); thePrint (1, Len); Puts (""); the } the + return 0; -}
Memory Search +dfs URAL 1183 Brackets Sequence