chillurbrain

11. Скобки (2)

May 22nd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. include <stdio.h>
  2. void pr(char *mas, int N)
  3. {
  4.     for(int i=0; i<N; i++)
  5.     {
  6.         printf("%c", mas[i]);
  7.  
  8.     }
  9.     printf("\n");
  10. }
  11.  
  12. void rec(char *mas, int j, int N, int temp1, int temp2)
  13. {
  14.     if(j==N)
  15.         pr(mas, N);
  16.     else
  17.     {
  18.     int t1=0, t2=0, i, fl=1;
  19.     for(i=j-1;fl && i>=0; i--)
  20.     {
  21.         if(mas[i]=='(') t1--;
  22.         if(mas[i]=='[') t2--;
  23.         if(mas[i]==')') t1++;
  24.         if(mas[i]==']') t2++;
  25.         if(t1<0 || t2<0)
  26.             fl=0;
  27.     }
  28.     if(fl)
  29.     {
  30.         mas[j]='(';
  31.         rec(mas, j+1, N, temp1+1, temp2);
  32.         mas[j]='[';
  33.         rec(mas, j+1, N, temp1, temp2+1);
  34.     }
  35.     else
  36.     {
  37.         if(t1<0)
  38.         {
  39.             mas[j]=')';
  40.             rec(mas, j+1, N, temp1, temp2);
  41.             if(temp1+temp2<N/2)
  42.             {
  43.                 mas[j]='(';
  44.                 rec(mas, j+1, N, temp1+1, temp2);
  45.                 mas[j]='[';
  46.                 rec(mas, j+1, N, temp1, temp2+1);
  47.             }
  48.         }
  49.         if(t2<0)
  50.         {
  51.             mas[j]=']';
  52.             rec(mas, j+1, N, temp1, temp2);
  53.             if(temp1+temp2<N/2)
  54.             {
  55.                 mas[j]='(';
  56.                 rec(mas, j+1, N, temp1+1, temp2);
  57.                 mas[j]='[';
  58.                 rec(mas, j+1, N, temp1, temp2+1);
  59.             }
  60.         }
  61.     }
  62.     }
  63. }
  64.  
  65.  
  66.  
  67. int main() {
  68.     int N;
  69.     char *mas;
  70.     scanf("%d", &N);
  71.     mas=new char[N];
  72.     mas[0]='(';
  73.     rec(mas, 1, N, 1, 0);
  74.     mas[0]='[';
  75.     rec(mas, 1, N, 0, 1);
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment