Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- ifstream in("paranteze.in");
- ofstream out("paranteze.out");
- char par[] = " ()";
- int n;
- struct {
- int x;
- char val;
- }st[21];
- int cont;
- int valid(int niv)
- {
- int i, j;
- for (i = 1; i<niv; i++) {
- if (st[i].val == '(') {
- st[i].x = 1;
- cont++;
- for (j = i + 1; j<niv; j++)
- if (st[j].val == ')'&& st[j].x==0 ) {
- st[j].x = 1;
- break;
- }
- }
- }
- for (i = 1; i<niv; i++)
- if (st[i].x == 0) return 0;
- if (cont>n / 2) return 0;
- return 1;
- }
- void cler() {
- for (int i = 1; i <= n; i++) {
- st[i].x = 0;
- }
- cont = 0;
- }
- void afisare_stiva()
- {
- int i;
- for (i = 1; i <= n; i++)
- out << st[i].val;
- out << '\n';
- }
- void back(int nivel)
- {
- if (nivel>n) {
- cler();
- if (valid(nivel)) afisare_stiva();
- }
- else
- {
- for (int i = 1; i <= 2; i++)
- {
- st[nivel].val = par[i];
- back(nivel + 1);
- }
- }
- }
- int main()
- {
- in >> n;
- back(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement