Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef int64_t ll;
- const int MX=2e5+6;
- int dp[55][55];
- string h;
- bool balanced(int x,int y)
- {
- if(h[x]=='('&&h[y]==')')return 1;
- if(h[x]=='['&&h[y]==']')return 1;
- if(x==y)return 1;
- return 0;
- }
- int correct(int x,int y)
- {
- if(h[x]=='('||h[x]=='[')return 1;
- if(h[y]==']'||h[y]==')')return 1;
- return 2;
- }
- int solve(int l=0,int r=(int)h.size()-1)
- {
- if(r-l==-1)return 0;
- if(r<l)return 1e5;
- if(l==r)return 1e5;
- if(dp[l][r]!=-1)return dp[l][r];
- int ans=1e5;
- if(balanced(l,r))ans=min(ans,solve(l+1,r-1));
- if(!balanced(l,r))ans=min(ans,correct(l,r)+solve(l+1,r-1));
- for(int i=l+1;i<r;i++)
- {
- if(balanced(l,i)&&balanced(i+1,r))
- {
- ans=min(ans,solve(l,i)+solve(i+1,r));
- }
- if(balanced(l,i)&&!balanced(i+1,r))
- {
- ans=min(ans,solve(l,i)+solve(i+1,r)+correct(i+1,r));
- }
- if(!balanced(l,i)&&balanced(i+1,r))
- {
- ans=min(ans,solve(l,i)+solve(i+1,r)+correct(l,i));
- }
- if(!balanced(l,i)&&!balanced(i+1,r))
- {
- ans=min(ans,solve(l,i)+solve(i+1,r)+correct(l,i)+correct(i+1,r));
- }
- }
- return dp[l][r]=ans;
- }
- int main()
- {
- memset(dp,-1,sizeof dp);
- cin>>h;
- cout<<solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement