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 Python (3.4.3) (judge verdict: Runtime Error)

#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define zeros(arr) memset(arr,0,sizeof(arr))
#define precise(num)  cout<<fixed; cout << std::setprecision(num)
int32_t main(){
    
    ios_base::sync_with_stdio(cin.tie(NULL));
    cout.tie(NULL);
    
    int tc = 1;
    // cin>>tc;
    while(tc -- ){
        
        vector< string >v;
        
        string p = "a";
        v.pb(p);
        int n;
        cin>>n;
        
        int curr = 'a';
        
        // vector< char >pos;
        // pos.pb(curr);
        vector< string>final;
        for(int i = 1 ;i < n ; i++ ){
            
            int sz =v.size(); 
            curr++;
            // pos.pb(curr);
            for(int j = 0; j < sz; j++ )
            {
                    vector< char > pos;
                    map<char,int>cn;
                    char p = 'a';
                    for(int k = 0; k < v[j].size() ; k ++ )
                        {
                            cn[v[j][k]]++;
                            p = max(p , v[j][k]);
                            if(cn[v[j][k]]==1)
                            pos.pb(v[j][k]);
                        }
                        pos.pb(p+1);
                    
                    for(int k = 0 ; k <pos.size() ; k++)
                        {
                            string  res = v[j] + pos[k];
                            
                            
                            v.pb(res);
                        
                            // if(i == n-1)
                            //     final.pb(res);
                        }
                        
            }
            
        }
        unordered_map<string , int > cn;
        for(int i = 0; i < v.size() ; i++ )
        {
            cn[v[i]]++;
            if(v[i].size() == n && cn[v[i]] == 1)
                final.pb(v[i]);
        }
        
        
        sort(all(final));
        
        for(int i =0 ; i< final.size() ; i ++ ){
            cout<<final[i]<<endl;
        }
        // if( n == 1)cout<<'a'<<endl;
        
        
    }
    
}