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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using ll = long long;
namespace mp = boost::multiprecision;
using mpint = mp::cpp_int; // 多倍長整数（任意精度）

/*
 * 定数
 */
const long double PI = (acos(-1));
const long double EPS = 1e-10;
const long long INF = 1e18;

/*
 * データ変換 
 */
/* char -> int */
int ctoi(char c) { return (int)(c - '0'); }

/*
 * 数学的な計算
 */
/* 階乗 */
int factorial(int n){ int ret = 1; for(int i = 1; i < n + 1; ++i) { ret = ret * i; } return ret; }

/* 最大公約数 */
template <typename T>T gcd(T x, T y){ return x % y == 0 ? y : gcd(y, x % y); }

/* 最小公倍数 */
template <typename T>T lcm(T a, T b) { return a * b / gcd(a, b); }

/* n個の整数の最小公倍数 */
template <typename T>T lcm_n(vector<T> &numbers) { T l; l = numbers[0]; for(int i = 1; i < numbers.size(); ++i) { l = lcm(l, numbers[i]); } return l; }

/*
 * DP向け
 */
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

/*
 * グラフ
 */
class Graph {
   public:
    vector<vector<int>> G;
    vector<int> Trans;
    vector<int> inDeg;
    int numV;
 
    Graph(int numV) {
        this->numV = numV;
        inDeg.assign(numV, 0);
        Trans.assign(numV, 0);
        G.assign(numV, vector<int>());
    }
 
    int topologicalSort(int v, vector<int> &dp) {
        if (dp[v] != -1) {
            return dp[v];
        }
 
        int res = 0;
 
        for (auto to : G[v]) {
            chmax(res, topologicalSort(to, dp) + 1);
        }
 
        return dp[v] = res;
    }
};

#define layer_name(n) l ## n

int main(){
  int N;
  cin >> N;

  // vector<vector<vector<bool>>> is_searched(N, vector<vector<bool>>(N, vector<bool>(1, false)));
  // vector<vector<vector<bool>>> parent(N, vector<vector<bool>>(N, vector<bool>(1, false)));

  // int depth = 0;
  // int alphabet = 0;


  // while(1){
  //   // 文字出力後、探索リストが全部trueなら親に戻る

  //   // まだ探索してないなら
  //   if(!is_searched[depth][alphabet])

  //   // N文字目がN番目のアルファベットなら終了する
  // }

  // 1こ前の層
  vector<string> l1(1, "a");
  vector<string> m1(1, "a");

  // ここは、"aa", "ab"がくる
  vector<string> l2(1, "a");
  vector<string> m2(1, "a");



  for(int i = 1; i < N; ++i){
    // l1にl2を格納する
    l1.resize(l2.size());
    m1.resize(m2.size());

    for(int l_index = 0; l_index < l2.size(); ++l_index){
      l1[l_index] = l2[l_index];
      m1[l_index] = m2[l_index];
    }
    l2.resize(0);
    m2.resize(0);
    // 新しいレイヤーの数を計算する
    // 前の層を全部見る
    for(int l1_index = 0; l1_index < l1.size(); ++l1_index){
      // 前の層のある値の最大文字からその層に伸びる数を計算する
      int d = m1[l1_index][0] - 'a';

      for(int j = 0; j < d+2; ++j){
        char tmp = 'a' + j;
        string c{tmp};
        l2.push_back(l1[l1_index] + c);

        if(m1[l1_index][0] < c[0]){
          m2.push_back(c);
        }else{
          m2.push_back(m1[l1_index]);
        }
      }
    }
  }

  for(auto v : l2){
    cout << v << endl;
  }


  return 0;
}