Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include <stdio.h>
- void pr(char *mas, int N)
- {
- for(int i=0; i<N; i++)
- {
- printf("%c", mas[i]);
- }
- printf("\n");
- }
- void rec(char *mas, int j, int N, int temp1, int temp2)
- {
- if(j==N)
- pr(mas, N);
- else
- {
- int t1=0, t2=0, i, fl=1;
- for(i=j-1;fl && i>=0; i--)
- {
- if(mas[i]=='(') t1--;
- if(mas[i]=='[') t2--;
- if(mas[i]==')') t1++;
- if(mas[i]==']') t2++;
- if(t1<0 || t2<0)
- fl=0;
- }
- if(fl)
- {
- mas[j]='(';
- rec(mas, j+1, N, temp1+1, temp2);
- mas[j]='[';
- rec(mas, j+1, N, temp1, temp2+1);
- }
- else
- {
- if(t1<0)
- {
- mas[j]=')';
- rec(mas, j+1, N, temp1, temp2);
- if(temp1+temp2<N/2)
- {
- mas[j]='(';
- rec(mas, j+1, N, temp1+1, temp2);
- mas[j]='[';
- rec(mas, j+1, N, temp1, temp2+1);
- }
- }
- if(t2<0)
- {
- mas[j]=']';
- rec(mas, j+1, N, temp1, temp2);
- if(temp1+temp2<N/2)
- {
- mas[j]='(';
- rec(mas, j+1, N, temp1+1, temp2);
- mas[j]='[';
- rec(mas, j+1, N, temp1, temp2+1);
- }
- }
- }
- }
- }
- int main() {
- int N;
- char *mas;
- scanf("%d", &N);
- mas=new char[N];
- mas[0]='(';
- rec(mas, 1, N, 1, 0);
- mas[0]='[';
- rec(mas, 1, N, 0, 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment