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 Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buffer []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buffer = append(buffer, line...)
		if !isPrefix {
			break
		}
	}
	return string(buffer)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	r := io.tokens[io.nextToken]
	io.nextToken++
	return r
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) PrintLn(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntLn(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.PrintLn(b...)
}

func (io *Io) PrintStringLn(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.PrintLn(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func intMin(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func intMax(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func nChar(str string) int {
	// fmt.Printf("-- str %v\n", str)
	max := 0
	for _, j := range []byte(str) {
		if max < int(j) {
			max = int(j)
		}
		// fmt.Printf("-- %v %d\n", i, j)
	}
	return max - 96
}

func main() {
	// var ap [100000]int
	// var bp [100000]int
	// var x, y, c, p int

	io := NewIo()
	defer io.Flush()
	N := io.NextInt()
	// res := "a"
	// fmt.Printf("%v\n", N)
	// fmt.Printf("%v\n", string(97))

	var tab map[int][]string
	tab = make(map[int][]string)
	tab[1] = []string{"a"}

	for i := 2; i <= N; i++ {
		tab[i] = []string{}
		// fmt.Printf("  %d --\n", i)
		for _, j := range tab[i-1] {
			// fmt.Println(j)
			nChar := nChar(j)
			// fmt.Printf("nChar %d %v\n", nChar, j)
			for k := 1; k <= nChar+1; k++ {
				tab[i] = append(tab[i], j+string(k+96))
			}
		}
	}
	// fmt.Printf("%v\n", tab[N])
	sort.Strings(tab[N])
	// fmt.Printf("%v\n", tab[N])
	for _, j := range tab[N] {
		io.PrintLn(j)
	}
	// // tab[2] = []string{"aa", "ab"}

	// // fmt.Printf("%v\n", tab[3])
	// // tab[3] = append(tab[3], "aaa")

	// strs := []string{"abcac", "zyxzx", "abaac", "abcbd"}
	// sort.Strings(strs)
	// for _, j := range strs {
	// 	io.PrintLn(j)
	// }
}