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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.*;
import java.math.*;
 
public class Main {
  private static int n;
  private static List<String> res = new LinkedList<String>();
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    dfs(new StringBuffer(), 0);
    Collections.sort(res, new Comparator<String> () {
      public int compare(String a, String b) {
        int len = Math.min(a.length(), b.length());
        for(int i = 0; i < len; ++i) {
          if(a.charAt(i) != b.charAt(i)) return a.charAt(i) - b.charAt(i);
        }
        return a.length() - b.length();
      }
    });
    for(String s : res) {
      System.out.println(s);
    }
  }
  
  private static void dfs(StringBuffer cur, int used) {
    if(cur.length() >= n) {
      res.add(cur.toString());
      return;
    }
    for(int i = 0; i <= Math.min(cur.length(), used); ++i) {
      cur.append((char)('a' + i));
      int next = used;
      if(i >= used) next = i + 1;
      dfs(cur, next);
      cur.deleteCharAt(cur.length() - 1);
    }
  }
}