Advertisement
Wooph

Paranteze

Mar 10th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ifstream in("paranteze.in");
  7. ofstream out("paranteze.out");
  8.  
  9. char par[] = " ()";
  10. int n;
  11. struct {
  12.     int x;
  13.     char val;
  14. }st[21];
  15. int cont;
  16. int valid(int niv)
  17. {
  18.     int i, j;
  19.     for (i = 1; i<niv; i++) {
  20.         if (st[i].val == '(') {
  21.             st[i].x = 1;
  22.             cont++;
  23.             for (j = i + 1; j<niv; j++)
  24.                 if (st[j].val == ')'&& st[j].x==0 ) {
  25.                     st[j].x = 1;
  26.                     break;
  27.                 }
  28.         }
  29.     }
  30.  
  31.     for (i = 1; i<niv; i++)
  32.         if (st[i].x == 0) return 0;
  33.     if (cont>n / 2) return 0;
  34.     return 1;
  35. }
  36. void cler() {
  37.     for (int i = 1; i <= n; i++) {
  38.         st[i].x = 0;
  39.     }
  40.     cont = 0;
  41. }
  42. void afisare_stiva()
  43. {
  44.     int i;
  45.     for (i = 1; i <= n; i++)
  46.         out << st[i].val;
  47.     out << '\n';
  48. }
  49. void back(int nivel)
  50. {
  51.     if (nivel>n) {
  52.         cler();
  53.         if (valid(nivel)) afisare_stiva();
  54.     }
  55.     else
  56.     {
  57.         for (int i = 1; i <= 2; i++)
  58.         {
  59.  
  60.             st[nivel].val = par[i];
  61.             back(nivel + 1);
  62.         }
  63.     }
  64. }
  65. int main()
  66. {
  67.     in >> n;
  68.     back(1);
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement