Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <stack>
- using namespace std;
- bool CheckBracketCorrect(string &str) {
- stack<char> br;
- for (auto el: str) {
- if (el == '(' || el == '[') {
- br.push(el);
- } else if (el == ')') {
- if (!br.empty() && br.top() == '(') {
- br.pop();
- } else {
- return false;
- }
- } else {
- if (!br.empty() && br.top() == '[') {
- br.pop();
- } else {
- return false;
- }
- }
- }
- if (br.empty()) {
- return true;
- }
- return false;
- }
- void
- PrintBracketPermutations(string cur_string, int n, int cur_len, vector<int> counts, vector<string> &add) {
- if (counts[2] > counts[0] || counts[0] - counts[2] > n - cur_len || counts[3] > counts[1] ||
- counts[1] - counts[3] > n - cur_len) {
- return;
- }
- if (cur_len == n) {
- if (CheckBracketCorrect(cur_string)) {
- cout << cur_string << '\n';
- }
- return;
- }
- bool flag = false;
- if (n - cur_len == counts[0] - counts[2]) {
- flag = true;
- ++counts[2];
- PrintBracketPermutations(cur_string + add[2], n, cur_len + 1, counts, add);
- --counts[2];
- }
- if (n - cur_len == counts[1] - counts[3]) {
- flag = true;
- ++counts[3];
- PrintBracketPermutations(cur_string + add[3], n, cur_len + 1, counts, add);
- --counts[3];
- }
- if (!flag) {
- for (int i = 0; i < add.size(); ++i) {
- ++counts[i];
- PrintBracketPermutations(cur_string + add[i], n, cur_len + 1, counts, add);
- --counts[i];
- }
- }
- }
- int main() {
- int n;
- cin >> n;
- vector<string> add = {"(", "[", ")", "]"};
- string cur_string;
- vector<int> vec = {0, 0, 0, 0};
- PrintBracketPermutations(cur_string, n, 0, vec, add);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement