Memory Search +dfs URAL 1183 Brackets Sequence

Source: Internet
Author: User

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

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.