poj2955 Brackets Simple Interval DP

Source: Internet
Author: User

Tag:poj2955   brackets    simple interval dp   

poj2955 simple interval dp//d[i][j] indicates the maximum matching sequence that can be formed by the I-to-J interval//dp[i][j] = max (Dp[i][k]+dp[k+1][j]) {i<k<j}//dp[i][j] = max (dp[i+ 1][j-1]+2) if (S[i] match s[j])////memory search, will dp[i][i] = 0, the other assignment to -1;////to do the problem when the beginning will dp[i][j] into 0, the results have been tle//finally found that there are a lot of state repeated calculation , so it is not difficult to tle//this problem, learning the importance of the initial boundary! Keep practicing .... #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 intMOD = 1000000007;const long Double PI = ACOs ( -1.L); Template<class t> inline T LCM (const t& A, const t& b) { Return A/GCD (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 = 128;char s[maxn];int d[maxn][maxn];int n;void init () {memset (d,-1,sizeof (d)); n = strlen (s);} BOOL Match (char A,char b) {if (a== ' (' &&b== ') ') return true;if (a== ' [' &&b== '] ') return True;return false;  int dp (int x,int y) {if (x>=y) return d[x][y]=0;if (d[x][y]>=0) return d[x][y];//d[x][y] = 0;int& ans = D[x][y];ans  = 0;if (Match (s[x],s[y))) ans = max (ANS,DP (x+1,y-1) + 2); for (int i = x;i<y;i++) ans = max (ANS,DP (x,i) +DP (i+1,y)); return Ans;} void Solve () {printf ("%d\n", DP (0,N-1));} int main () {//freopen ("G:\\code\\1.txt", "R", stdin), while (gets (s)) {if (s[0]== ' e ')Break;init (); Solve ();} return 0;}

poj2955 Brackets Simple Interval DP

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.