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)

#pragma GCC optimize("O3", "unroll-loops")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;
#define ull unsigned long long
#define si short int
#define ll long long int
#define ld long double
#define pb push_back
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define piii pair<pair<int, int>, int>
#define all(vec) vec.begin(), vec.end()
#define piiii pair<pair<int, int>, pair<int, int>>
#define fr(a, from, c) for(int a=(from); (a)<(c); (a)++)

/*-----------------------------------------------------SegTree---------------------------------------------*/
/*int mxn11 = (int) (2e5 + 1);
vector<ll> t(4 * mxn11 + 1, 0);
vector<ll> tAdd(4 * mxn11 + 1, 0);

void build(int v, int vl, int vr) {
    if (vl == vr) {
        t[v] = vl;
    } else {
        int tmp = (vl + vr) >> 1;
        build(2 * v, vl, tmp);
        build(2 * v + 1, tmp + 1, vr);
        t[v] = max(t[2 * v], t[2 * v + 1]);
    }
}

*//* void push(int v) {
     tAdd[2 * v] += tAdd[v];
     tAdd[2 * v + 1] += tAdd[v];
     t[v] += tAdd[v];
     tAdd[v] = 0;
 }*//*

int get(int v, int vl, int vr, int l, int r) {
    if (l > r) return 0;
    if (vl == l && vr == r) {
        return t[v] + tAdd[v];
    }
    int tmp = (vl + vr) >> 1;
    // push(v);
    ll a = get(2 * v, vl, tmp, l, min(tmp, r));
    ll b = get(2 * v + 1, tmp + 1, vr, max(tmp + 1, l), r);
    return max(a, b);
}

void update(int v, int vl, int vr, int pos, int cnt) {
    if (vl == vr) {
        t[v] = cnt;
        return;
    }
    int tmp = (vl + vr) >> 1;
    //push(v);
    if (pos <= tmp) {
        update(2 * v, vl, tmp, pos, cnt);
    } else {
        update(2 * v + 1, tmp + 1, vr, pos, cnt);
    }
    t[v] = max(t[2 * v], t[2 * v + 1]);
}*/

/*-----------------------------------------------------SegTree---------------------------------------------*/
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

/*-----------------------------------------------------MATH------------------------------------------------*/
inline ll gcd(ll a, ll b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }


inline ll gcdex(ll a, ll b, ll &x, ll &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll xx, yy;
    ll gc = gcdex(b, a % b, yy, xx);
    x = xx;
    y = yy - (a / b) * xx;
    return gc;
}

inline ll pwm(ll xx, ll pow, ll MD) {
    if (pow < 0) { pow = pow % (MD - 1) + MD - 1; }
    ll mlt = 1;
    while (pow) {
        if (pow & 1) {
            mlt *= xx;
            mlt %= MD;
        }
        xx *= xx;
        pow >>= 1;
        xx %= MD;
    }
    return mlt;
}
inline int inv(ll r, int _mod) { return pwm(r % _mod, _mod - 2, _mod); }

inline int64_t hilbertOrder(int x, int y, int pow, int rotate) {
    if (pow == 0) {
        return 0;
    }
    int hpow = 1 << (pow - 1);
    int seg = (x < hpow) ? (
            (y < hpow) ? 0 : 3
    ) : (
                      (y < hpow) ? 1 : 2
              );
    seg = (seg + rotate) & 3;
    const int rotateDelta[4] = {3, 0, 0, 1};
    int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
    int nrot = (rotate + rotateDelta[seg]) & 3;
    int64_t subSquareSize = int64_t(1) << (2 * pow - 2);
    int64_t ans = seg * subSquareSize;
    int64_t add = hilbertOrder(nx, ny, pow - 1, nrot);
    ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
    return ans;
}

/*-----------------------------------------------------MATH------------------------------------------------*/

vector<string> ans;
void choose(char c, int kolvo, string res, int after){
    bool ok=true;
    int pust=0;
    int first=-1;
    for(int i=0;i<res.size();i++){
        if(res[i]=='-'){
            if(ok)first=i;
            ok=false;
            pust++;
        }
    }
    if(ok && kolvo==0) {
        ans.pb(res);
        return;
    }
    if(ok){
        return;
    }
    if(kolvo==0){
        c=char(c+1);
        res[first]=c;
        for(int i=1;i<=pust;i++){
            choose(c, i-1, res, first+1);
        }
    }
    else{
        for(int i=after;i<res.size();i++){
            if(res[i]=='-'){
                res[i]=c;
                choose(c, kolvo-1, res, i+1);
            }
        }
    }
}
void solve() {
    int n;
    cin>>n;
    for(int a=1;a<=n;a++){
        string res;
        res.assign(n,'-');
        res[0]='a';
        // choose pos for a
        choose('a', a-1, res, 1);
    }
    sort(all(ans));
    ans.erase(unique(ans.begin(), ans.end()), ans.end());
    for(auto s:ans) cout<<s<<endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    //freopen("C:\\Users\\Maximus\\CLionProjects\\Codeforces\\BestTeam\\output.txt", "r", stdin);
    // f=fopen("C:\\Users\\Maximus\\CLionProjects\\Codeforces\\BestTeam\\output.txt", "w");
    //freopen("C:\\Users\\Maximus\\CLionProjects\\Codeforces\\BestTeam\\output.txt", "w", stdout);
    unsigned int beg_time = clock();
    int n=1;
    for (int i = 0; i < n; i++) solve();


    unsigned int end_time = clock();
    //  cout<<endl<<endl<<end_time-beg_time;

    return 0;
}