Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- long total = 0;
- void brackets(std::string& source, std::string s, int openned, int closed, int depth) {
- if (source.size()%2) {
- return;
- }
- if (depth > source.size()) {
- return;
- }
- if ((openned - closed == 0) && (s.size() == source.size())) {
- if ((s[0] != ')') && (s[s.size()-1] != '(')) {
- int checkBal = 0;
- for (int i = 0; i < s.size(); ++i) {
- if (s[i] == '(') checkBal++;
- if (s[i] == ')') checkBal--;
- if (checkBal < 0) break;
- }
- if (checkBal < 0) return;
- total++;
- }
- }
- if (source[depth] == '(') {
- brackets(source, s+="(", openned+1, closed, depth+1);
- }
- if (source[depth] == ')') {
- brackets(source, s+=")", openned, closed+1, depth+1);
- }
- if (source[depth] == '?') {
- brackets(source, s+="(", openned+1, closed, depth+1);
- s[s.size()-1] = '\0';
- s.resize(s.size()-1);
- brackets(source, s+=")", openned, closed+1, depth+1);
- s[s.size()-1] = '\0';
- s.resize(s.size()-1);
- }
- }
- int main() {
- std::ifstream FileIn("./input.txt");
- std::string source;
- FileIn >> source;
- //std::cout << source;
- brackets(source, "", 0, 0, 0);
- std::cout << total << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement