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 <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

typedef long long int ll;
typedef pair<int, int> Pii;

const ll mod = 1000000007;

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

  int n;
  cin >> n;

  vector<vector<int> > ans;
  queue<pair<vector<int>, int> > que;
  que.emplace(vector<int>(), 0);
  while (!que.empty()) {
    auto now = que.front();
    que.pop();
    if (now.first.size() == n) {
      ans.push_back(now.first);
      continue;
    }
    for (int i = 0; i < now.second; i++) {
      vector<int> next = now.first;
      next.push_back(i);
      que.emplace(next, now.second);
    }
    vector<int> next = now.first;
    next.push_back(now.second);
    que.emplace(next, now.second+1);
  }

  sort(ans.begin(), ans.end());

  for (auto &x: ans) {
    for (auto &y: x) {
      char next = 'a' + y;
      cout << next;
    }
    cout << endl;
  }

  return 0;
}