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 Rust (1.42.0) (judge verdict: Accepted)

#![allow(non_snake_case)]
#![allow(unused_imports)]
use ascii::AsciiChar;
use ascii::AsciiStr;
use itertools::Itertools as _;
use permutohedron::LexicalPermutation as _;
use petgraph::algo::dijkstra;
use petgraph::algo::min_spanning_tree;
use petgraph::data::FromElements;
use petgraph::graph::NodeIndex;
use petgraph::graph::UnGraph;
use petgraph::unionfind::UnionFind;
use proconio::fastout;
use proconio::input;
use proconio::marker::Chars;
use proconio::marker::Usize1;
use std::cmp::max;
use std::cmp::min;
use std::collections::HashMap;
use std::collections::VecDeque;
use superslice::Ext as _;

#[fastout]
fn main() {
    input! {
        N: usize,
    }

    // a
    // |aa
    // |ab
    // |aaa
    // |aab
    // |aba
    // |abb
    // |abc

    // lunlun_numberみたいにキューに突っ込んで無限に生成していく

    let mut q: VecDeque<(Vec<char>, u8)> = VecDeque::new();
    let initial = (vec!['a'], 'a' as u8);
    q.push_back(initial);

    let mut anss: Vec<String> = vec![];

    while !q.is_empty() {
        let (chars, largest_ch_u8) = q.pop_front().unwrap();

        if chars.len() == N {
            let s: String = chars.iter().collect();
            anss.push(s);
        } else if chars.len() < N {
            for c in ('a' as u8)..=largest_ch_u8 {
                let mut next_chars = chars.clone();
                next_chars.push(c as char);
                let tpl = (next_chars, largest_ch_u8);
                q.push_back(tpl);
            }

            let c = largest_ch_u8 + 1;
            let mut next_chars = chars.clone();
            next_chars.push(c as char);
            let tpl = (next_chars, c);
            q.push_back(tpl);
        }
    }

    for ans in anss.iter() {
        println!("{}", ans);
    }
}