uva1626 POJ 1141 Brackets Sequence interval DP print path

Source: Internet
Author: User

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&GT;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&GT;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.