Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n, st[30];
- /**lg. stiva = n; st[i] = 1, 2; nr1 >= nr2; nr1 <= n/2;*/
- void Afisare()
- {
- for(int i = 1; i <= n; i++)
- if(st[i] == 1) cout << "(";
- else cout << ")";
- cout << "\n";
- }
- int Valid(int top)
- {
- int i, nr1 = 0;
- for(i = 1; i <= top; i++)
- if(st[i] == 1) nr1++;
- if(nr1 < top - nr1) return 0;
- if(nr1 > n / 2) return 0;
- return 1;
- }
- void Back()
- {
- int cand, top;
- top = 1; st[top] = 0;
- while(top > 0)
- {
- cand = 0;
- while(cand == 0 && st[top] < 2)
- {
- st[top]++;
- cand = Valid(top);
- }
- if(cand == 0) top--;
- else if(top == n) Afisare();
- else st[++top] = 0;
- }
- }
- int main()
- {
- cin >> n;
- Back();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement