Problem:
Score : 400 points

Problem Statement

In this problem, we only consider strings consisting of lowercase English letters.

Strings s and t are said to be isomorphic when the following conditions are satisfied:

|s| = |t| holds.

For every pair i, j, one of the following holds:

s_i = s_j and t_i = t_j.

s_i \neq s_j and t_i \neq t_j.

For example, abcac and zyxzx are isomorphic, while abcac and ppppp are not.

A string s is said to be in normal form when the following condition is satisfied:

For every string t that is isomorphic to s, s \leq t holds. Here \leq denotes lexicographic comparison.

For example, abcac is in normal form, but zyxzx is not since it is isomorphic to abcac, which is lexicographically smaller than zyxzx.

You are given an integer N.
Print all strings of length N that are in normal form, in lexicographically ascending order.

Constraints

1 \leq N \leq 10

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Assume that there are K strings of length N that are in normal form: w_1, \ldots, w_K in lexicographical order.
Output should be in the following format:

w_1
:
w_K

Sample Input 1

1

Sample Output 1

a

Sample Input 2

2

Sample Output 2

aa
ab

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<stdio.h>
int max(int* matr,int s)
{
  int i,x;
  x=*matr;
  for(i=1;i<s;i++)
  {
    if(*(matr+i)>x) x=*(matr+i);
  }
  return x;
}
int main()
{
  int a,b,c,d,e,f,g,h,i,j,n;
  char x,y,z;
  scanf("%d",&n);
  char arr[n];
  int matr[n];
  for(i=0;i<n;i++) 
  {
    arr[i]='a';
    matr[i]=0;
  }
  for(a=1;a<2;a++)
  {
    matr[0]=a;
    printf("%c",96+matr[0]);
    if(n>1)
    {
      for(b=1;b<3;b++)
      {
        matr[1]=b;
        printf("%c",96+matr[1]);
        if(n>2)
        {
          for(c=1;(c<4)&&(c<=(max(matr,2)+1));c++)
          {
            matr[2]=c;
            printf("%c",96+matr[2]);
            if(n>3)
            {
              for(d=1;(d<5)&&(d<=(max(matr,3)+1));d++)
              {
                matr[3]=d;
                printf("%c",96+matr[3]);
                if(n>4)
                {
                  for(e=1;(e<6)&&(e<=(max(matr,4)+1));e++)
                  {
                    matr[4]=e;
                    printf("%c",96+matr[4]);
                    if(n>5)
                    {
                      for(f=1;(f<7)&&(f<=(max(matr,5)+1));f++)
                      {
                        matr[5]=f;
                        printf("%c",96+matr[5]);
                        if(n>6)
                        {
                          for(g=1;(g<8)&&(g<=(max(matr,6)+1));g++)
                          {
                            matr[6]=g;
                            printf("%c",96+matr[6]);
                            if(n>7)
                            {
                              for(h=1;(h<9)&&(h<=(max(matr,7)+1));h++)
                              {
                                matr[7]=h;
                                printf("%c",96+matr[7]);
                                if(n>8)
                                {
                                  for(i=1;(i<10)&&(i<=(max(matr,8)+1));i++)
                                  {
                                    matr[8]=i;
                                    printf("%c",96+matr[8]);
                                    if(n>9)
                                    {
                                      for(j=1;(j<10)&&(j<=(max(matr,9)+1));j++)
                                      {
                                         matr[9]=j;
                                         printf("%c",96+matr[9]);
                                         printf("\n");
                                      }
                                    }
                                    else printf("\n");
                                  }
                                }
                                else printf("\n");
                              }
                            }
                            else printf("\n");
                          }
                        }
                        else printf("\n");
                      }
                    }
                    else printf("\n");
                  }
                }
                else printf("\n");
              }
            }
            else printf("\n");
          }
        }
        else printf("\n");
      }
    }
    else printf("\n");
  }
  return 0;
}