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: Accepted)

#include <stdio.h>
#include <string.h>

char strmax(char *str, int len)
{
	int index = 0;
	char max_c = str[0];
	while (index < len)
	{
		if(max_c < str[index])
			max_c = str[index];
		index++;
	}
	return (max_c);
}

int isvalid(char *str, int n)
{
	int index = 1;
	int j = 0;
	if (str[0] != 'a')
		return (0);
	while (index < n)
	{
		if (strmax(str,index) + 1 < str[index])
			return(0);
		index++;
	}
	return (1);
}

int	main(void)
{
	char str[] = "aaaaaaaaaa";
	int n = 0;
	int index = 0;
	int j,k,l,m,o,p,q,r,s,t;

	scanf("%d", &n);
	if (n == 1)
	{
		printf("a\n");
		return 0;
	}
	str[n] = '\0';
	if (n == 2)
	{
		printf("aa\nab\n");
		return (0);
	}
	if (n == 3)
	{
		q=0;
		while(q<n-2)
		{
			str[n-3] = str[n-3] +q;
			r=0;
			while(r<n-1)
			{
				str[n-2] = str[n-2] +r;
				s=0;
				while(s<n)
				{
					str[n-1] = str[n-1]+s;
					if(isvalid(str,3) == 1)
						printf("%s\n",str);
					str[n-1] = 'a';
					s++;
				}
				str[n-2] = 'a';
				r++;
			}
			str[n-3] = 'a';
			q++;
		}
		return 0;
	}
if (n == 4)
	{
		p=0;
		while(p<n-3)
		{
			str[n-4] = str[n-4] +p;
			q=0;
			while(q<n-2)
			{
				str[n-3] = str[n-3] +q;
				r=0;
				while(r<n-1)
				{
					str[n-2] = str[n-2] +r;
					s=0;
					while(s<n)
					{
						str[n-1] = str[n-1]+s;
						if(isvalid(str,n) == 1)
							printf("%s\n",str);
						str[n-1] = 'a';
						s++;
					}
					str[n-2] = 'a';
					r++;
				}
				str[n-3] = 'a';
				q++;
			}
			str[n-4] = 'a';
			p++;
		}
		return 0;
	}
if (n == 5)
	{
		o=0;
		while(o<n-4)
		{
			str[n-5] = str[n-5] +o;
			p=0;
			while(p<n-3)
			{
				str[n-4] = str[n-4] +p;
				q=0;
				while(q<n-2)
				{
					str[n-3] = str[n-3] +q;
					r=0;
					while(r<n-1)
					{
						str[n-2] = str[n-2] +r;
						s=0;
						while(s<n)
						{
							str[n-1] = str[n-1]+s;
							if(isvalid(str,n) == 1)
								printf("%s\n",str);
							str[n-1] = 'a';
							s++;
						}
						str[n-2] = 'a';
						r++;
					}
					str[n-3] = 'a';
					q++;
				}
				str[n-4] = 'a';
				p++;
			}
			str[n-5] = 'a';
			o++;
		}
		return 0;
	}
if (n == 6)
	{
		m=0;
		while(m<n-5)
		{
			str[n-6] = str[n-6] +m;
			o=0;
			while(o<n-4)
			{
				str[n-5] = str[n-5] +o;
				p=0;
				while(p<n-3)
				{
					str[n-4] = str[n-4] +p;
					q=0;
					while(q<n-2)
					{
						str[n-3] = str[n-3] +q;
						r=0;
						while(r<n-1)
						{
							str[n-2] = str[n-2] +r;
							s=0;
							while(s<n)
							{
								str[n-1] = str[n-1]+s;
								if(isvalid(str,n) == 1)
									printf("%s\n",str);
								str[n-1] = 'a';
								s++;
							}
							str[n-2] = 'a';
							r++;
						}
						str[n-3] = 'a';
						q++;
					}
					str[n-4] = 'a';
					p++;
				}
				str[n-5] = 'a';
				o++;
			}
			str[n-6] = 'a';
			m++;
		}
		return 0;
	}

	if (n == 7)
	{
		l=0;
		while(l<n-6)
		{
			str[n-7] = str[n-7] + l;
			m=0;
			while(m<n-5)
			{
				str[n-6] = str[n-6] +m;
				o=0;
				while(o<n-4)
				{
					str[n-5] = str[n-5] +o;
					p=0;
					while(p<n-3)
					{
						str[n-4] = str[n-4] +p;
						q=0;
						while(q<n-2)
						{
							str[n-3] = str[n-3] +q;
							r=0;
							while(r<n-1)
							{
								str[n-2] = str[n-2] +r;
								s=0;
								while(s<n)
								{
									str[n-1] = str[n-1]+s;
									if(isvalid(str,n) == 1)
										printf("%s\n",str);
									str[n-1] = 'a';
									s++;
								}
								str[n-2] = 'a';
								r++;
							}
							str[n-3] = 'a';
							q++;
						}
						str[n-4] = 'a';
						p++;
					}
					str[n-5] = 'a';
					o++;
				}
				str[n-6] = 'a';
				m++;
			}
			str[n-7] = 'a';
			l++;
		}
		return 0;
	}

	if (n == 8)
	{
		k=0;
		while (k<n-7)
		{
			str[n-8] = str[n-8] + k;
			l=0;
			while(l<n-6)
			{
				str[n-7] = str[n-7] + l;
				m=0;
				while(m<n-5)
				{
					str[n-6] = str[n-6] +m;
					o=0;
					while(o<n-4)
					{
						str[n-5] = str[n-5] +o;
						p=0;
						while(p<n-3)
						{
							str[n-4] = str[n-4] +p;
							q=0;
							while(q<n-2)
							{
								str[n-3] = str[n-3] +q;
								r=0;
								while(r<n-1)
								{
									str[n-2] = str[n-2] +r;
									s=0;
									while(s<n)
									{
										str[n-1] = str[n-1]+s;
										if(isvalid(str,n) == 1)
											printf("%s\n",str);
										str[n-1] = 'a';
										s++;
									}
									str[n-2] = 'a';
									r++;
								}
								str[n-3] = 'a';
								q++;
							}
							str[n-4] = 'a';
							p++;
						}
						str[n-5] = 'a';
						o++;
					}
					str[n-6] = 'a';
					m++;
				}
				str[n-7] = 'a';
				l++;
			}
			str[n-8] = 'a';
			k++;
		}
		return 0;
	}
if (n == 9)
	{
		j=0;
		while (j<n-8)
		{
			str[n-9] = str[n-9] + j;
			k=0;
			while (k<n-7)
			{
				str[n-8] = str[n-8] + k;
				l=0;
				while(l<n-6)
				{
					str[n-7] = str[n-7] + l;
					m=0;
					while(m<n-5)
					{
						str[n-6] = str[n-6] +m;
						o=0;
						while(o<n-4)
						{
							str[n-5] = str[n-5] +o;
							p=0;
							while(p<n-3)
							{
								str[n-4] = str[n-4] +p;
								q=0;
								while(q<n-2)
								{
									str[n-3] = str[n-3] +q;
									r=0;
									while(r<n-1)
									{
										str[n-2] = str[n-2] +r;
										s=0;
										while(s<n)
										{
											str[n-1] = str[n-1]+s;
											if(isvalid(str,n) == 1)
												printf("%s\n",str);
											str[n-1] = 'a';
											s++;
										}
										str[n-2] = 'a';
										r++;
									}
									str[n-3] = 'a';
									q++;
								}
								str[n-4] = 'a';
								p++;
							}
							str[n-5] = 'a';
							o++;
						}
						str[n-6] = 'a';
						m++;
					}
					str[n-7] = 'a';
					l++;
				}
				str[n-8] = 'a';
				k++;
			}
			str[n-9] = 'a';
			j++;
		}
		return 0;
	}

	if (n == 10)
	{
		j=0;
		while (j<n-8)
		{
			str[n-9] = str[n-9] + j;
			k=0;
			while (k<n-7)
			{
				str[n-8] = str[n-8] + k;
				l=0;
				while(l<n-6)
				{
					str[n-7] = str[n-7] + l;
					m=0;
					while(m<n-5)
					{
						str[n-6] = str[n-6] +m;
						o=0;
						while(o<n-4)
						{
							str[n-5] = str[n-5] +o;
							p=0;
							while(p<n-3)
							{
								str[n-4] = str[n-4] +p;
								q=0;
								while(q<n-2)
								{
									str[n-3] = str[n-3] +q;
									r=0;
									while(r<n-1)
									{
										str[n-2] = str[n-2] +r;
										s=0;
										while(s<n)
										{
											str[n-1] = str[n-1]+s;
											if(isvalid(str,n) == 1)
												printf("%s\n",str);
											str[n-1] = 'a';
											s++;
										}
										str[n-2] = 'a';
										r++;
									}
									str[n-3] = 'a';
									q++;
								}
								str[n-4] = 'a';
								p++;
							}
							str[n-5] = 'a';
							o++;
						}
						str[n-6] = 'a';
						m++;
					}
					str[n-7] = 'a';
					l++;
				}
				str[n-8] = 'a';
				k++;
			}
			str[n-9] = 'a';
			j++;
		}
		return 0;
	}
}