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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;

char s[maxn];
int N;
bool vis[maxn];

void print(){
	for(int i=0;i<N;i++){
		cout<<s[i];
	}
	cout<<"\n";
}

void init(){
	for(int i=0;i<N;i++){
		s[i]='a';
	}
}

bool check(){
	memset(vis,0,sizeof(vis));
	for(int i=0;i<N-1;i++){
		if(s[i]>s[i+1]&&!vis[s[i+1]]){
			return true;
		}
//		cout<<"s["<<i<<"]="<<s[i]<<'\n';
		vis[s[i]]=true;
	}
	return false;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>N;
	init();
	print();
	if(N>1){
		init();
		s[N-1]='b';
		do{
			if(check()){
				break;
			}
			print();
		}while(next_permutation(s,s+N));
	}
	if(N>2){
		init();
		s[N-2]='b';
		s[N-1]='c';
		do{
			if(check()){
				break;
			}
			print();
		}while(next_permutation(s,s+N));
	}
	if(N>3){
		init();
		s[N-3]='b';
		s[N-2]='c';
		s[N-1]='d';
		do{
			if(check()){
				break;
			}
			print();
		}while(next_permutation(s,s+N));
	}
	if(N>4){
		init();
		s[N-4]='b';
		s[N-3]='c';
		s[N-2]='d';
		s[N-1]='e';
		do{
			if(check()){
				break;
			}
			print();
		}while(next_permutation(s,s+N));
	}
	if(N>5){
		init();
		s[N-5]='b';
		s[N-4]='c';
		s[N-3]='d';
		s[N-2]='e';
		s[N-1]='f';
		do{
			if(check()){
				break;
			}
			print();
		}while(next_permutation(s,s+N));
	}
	if(N>6){
		init();
		s[N-6]='b';
		s[N-5]='c';
		s[N-4]='d';
		s[N-3]='e';
		s[N-2]='f';
		s[N-1]='g';
		do{
			if(check()){
				break;
			}
			print();
		}while(next_permutation(s,s+N));
	}
	if(N>7){
		init();
		s[N-7]='b';
		s[N-6]='c';
		s[N-5]='d';
		s[N-4]='e';
		s[N-3]='f';
		s[N-2]='g';
		s[N-1]='h';
		do{
			if(check()){
				break;
			}
			print();
		}while(next_permutation(s,s+N));
	}
	if(N>8){
		init();
		s[N-8]='b';
		s[N-7]='c';
		s[N-6]='d';
		s[N-5]='e';
		s[N-4]='f';
		s[N-3]='g';
		s[N-2]='h';
		s[N-1]='i';
		do{
			if(check()){
				break;
			}
			print();
		}while(next_permutation(s,s+N));
	}
	if(N>9){
		init();
		s[N-9]='b';
		s[N-8]='c';
		s[N-7]='d';
		s[N-6]='e';
		s[N-5]='f';
		s[N-4]='g';
		s[N-3]='h';
		s[N-2]='i';
		s[N-1]='j';
		do{
			if(check()){
				break;
			}
			print();
		}while(next_permutation(s,s+N));
	}
	return 0;
}