Advertisement
HabKaffee

Untitled

Oct 16th, 2021
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. long total = 0;
  5.  
  6. void brackets(std::string& source, std::string s, int openned, int closed, int depth) {
  7. if (source.size()%2) {
  8. return;
  9. }
  10. if (depth > source.size()) {
  11. return;
  12. }
  13. if ((openned - closed == 0) && (s.size() == source.size())) {
  14. if ((s[0] != ')') && (s[s.size()-1] != '(')) {
  15. int checkBal = 0;
  16. for (int i = 0; i < s.size(); ++i) {
  17. if (s[i] == '(') checkBal++;
  18. if (s[i] == ')') checkBal--;
  19. if (checkBal < 0) break;
  20. }
  21. if (checkBal < 0) return;
  22. total++;
  23. }
  24. }
  25. if (source[depth] == '(') {
  26. brackets(source, s+="(", openned+1, closed, depth+1);
  27. }
  28. if (source[depth] == ')') {
  29. brackets(source, s+=")", openned, closed+1, depth+1);
  30. }
  31. if (source[depth] == '?') {
  32. brackets(source, s+="(", openned+1, closed, depth+1);
  33. s[s.size()-1] = '\0';
  34. s.resize(s.size()-1);
  35. brackets(source, s+=")", openned, closed+1, depth+1);
  36. s[s.size()-1] = '\0';
  37. s.resize(s.size()-1);
  38. }
  39. }
  40.  
  41. int main() {
  42. std::ifstream FileIn("./input.txt");
  43. std::string source;
  44. FileIn >> source;
  45. //std::cout << source;
  46. brackets(source, "", 0, 0, 0);
  47. std::cout << total << std::endl;
  48. return 0;
  49. }
Advertisement
RAW Paste Data Copied
Advertisement