bhupixb

number of balanced bracket subsequences

Jan 24th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. // I can't tell you what it really is,
  2. // I can only tell you what it feels like.
  3. #include "bits/stdc++.h"
  4. using namespace std;
  5.  
  6. const int N = 100;
  7.  
  8. int dp[N][N], n;
  9. char s[N];
  10.  
  11. int go(int ind, int bal) {
  12. if (bal < 0) return 0;
  13. if (!ind and !bal) return 1;
  14. if (ind < 1) return 0;
  15. if (dp[ind][bal] != -1) return dp[ind][bal];
  16. int x = go(ind-1, bal + (s[ind] == '(' ? -1 : 1));
  17. x += go(ind-1, bal);
  18. return x;
  19. }
  20.  
  21. signed main() {
  22. memset(dp, -1, sizeof dp);
  23. cin >> s+1;
  24. n = strlen(s+1);
  25. // cout << (s+1) << '\n';
  26. cout << go(n,0);
  27. return 0;
  28. }
  29. // 1). Try forming/rearranging the equations if any
  30. // 2). If problem asks to count, try counting in reverse(it can be easier sometimes)
Add Comment
Please, Sign In to add comment