POJ 1141 Brackets sequence//is also in the purple book to see a problem, UVA is more than a T group of data. The classic interval dp//dp (I,J) represents the number of parentheses required in the interval [i,j]//is divided into two cases//one is s[i] and s[j] is matched//dp[i][j] = min (dp[i][j],dp[i+1][j-1])// Another case is mismatch//dp[i][j] = min (Dp[i][j],dp[i][k]+dp[k+1][j]) {i<k<j};//But in any case the second case is to be made//such as [] this situation//// This question is actually quite simple, but is the printing method may be a bit complex//recursive printing really is a magical thing ah////by the way, the data a bit pit, there are empty strings of the situation, I wa for nearly one hours////learned the recursive printing, simple interval dp////ah, continue to practice it .... #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat > #include <climits> #include <cmath> #include <complex> #include <cstdio> #include < cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <vector> #define CEIL (A, B) (((a) + (b)-1)/(c)) #define Endl ' \ n ' #define GCD __gcd#define highbit (x) (1ull<< (63-__builtin_clzll (x))) #define Popcount __builtin_popcountlltypedef Long long ll;using namespace Std;const int MOD = 10000 00007;const long Double PI = ACOs ( -1.L); Template<class t> inline T LCM (const t& A, const t& b) {return A/GC D (A, b) *b; }template<class t> Inline T lowbit (const t& x) {return x&-x;} Template<class t> inline T maximize (t& A, const t& b) {return a=a<b?b:a;} Template<class t> inline T Minimize (t& A, const t& b) {return a=a<b?a:b;} const int MAXN = 1008;int d[maxn][maxn];char s[maxn];int n;const int inf = 0x4f4f4f4f;bool match (char A,char b) {if (a== ' (' && b== ') ' Return true;if (a== ' [' && b== '] ') return True;return false;} int dp (int i,int j) {if (i==j) return d[i][j];if (I>J) return d[i][j] = 0;if (d[i][j]!=inf) return d[i][j];int& ans = d I [J];if (Match (s[i],s[j])) ans = min (ANS,DP (i+1,j-1)); for (int k=i;k<j;k++) ans = min (ans,dp (i,k) +DP (k+1,j)); return Ans;} void print (int i,int j) {if (I>J) return, if (i==j) {if (s[i]== ' | | | s[i]== ') ("()") printf ("()"), Else printf ("[]"); return;} int ans = d[i][j];if (Match (s[i],s[j) && ans = = d[i+1][j-1]) {printf ("%c", S[i]);p rint (i+1,j-1);p rintf ("%c", s[j ]); return;} for (int k=i;k<j;k++) {if (ans = = D[i][k]+d[k+1][j]) {print (i,k);p rint (k+1,j); return;}} void Init () {n = strlen (s); for (int i=0;i<=n;i++) for (int j=0;j<=n;j++) d[i][j] = inf;for (int i=0;i<=n;i++) {d[i][ I] = 1;} DP (0,n-1);p rint (0,n-1);p UTS (""); int main () {//freopen ("G:\\code\\1.txt", "R", stdin), while (gets (s)) {if (s[0]) init (); Else puts ("");} return 0;}
uva1626 POJ 1141 Brackets Sequence interval DP print path