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



Sample input:
1


Sample output:
a

Submission status: Accepted

Solution code:
import string

alphabet = string.ascii_lowercase

n = int(input())

def DFS(before,keta):
    num = 0
    if keta == n:
        print(before)
        return
    for i in before:
        num = max(num,alphabet.index(i))
    num += 1
    for j in range(num+1):
        DFS(before+alphabet[j],keta+1)

DFS("a",1)

Submission status: Accepted

Solution code:
N = int(input())

def dfs(s, mx):
    if len(s) == N:
        print(s)
    else:
        c = 'a'
        for i in range(ord('a'), mx + 1):
            c = chr(i)
            if c == chr(mx):
                dfs(s + c, mx + 1)
            else:
                dfs(s + c, mx)

dfs('', ord('a'))

Submission status: Accepted

Solution code:
from collections import deque
import sys
N = int(input())

if N == 1:
	print('a')
	sys.exit()
else:
	ans = []
	def solve(s,m):
		if len(s) == N:
			ans.append(s)
			return 
		for n in range(97, m+2):
			solve(s + chr(n), max(n,m))
	solve('a',97)
	for a in ans:
		print(a)

Submission status: Accepted

Solution code:
def main():
    from string import ascii_lowercase
    dic = {i: s for i, s in enumerate(ascii_lowercase)}
    N = int(input())
    ans = []

    def dfs(s, mx):
        if len(s) == N:
            ans.append(s)
            return
        else:
            for i in range(mx+2):
                mx = max(mx, i)
                dfs(s + dic[i], mx)
    dfs("a", 0)
    print(*ans, sep="\n")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    from string import ascii_lowercase
    dic = {i: s for i, s in enumerate(ascii_lowercase)}
    N = int(input())
    ans = []

    def dfs(s, mx):
        if len(s) == N:
            ans.append(s)
            return
        else:
            for i in range(mx+2):
                v = s + dic[i]
                mx = max(mx, i)
                dfs(v, mx)
    dfs("a", 0)
    print(*ans, sep="\n")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    from string import ascii_lowercase
    dic = {i: s for i, s in enumerate(ascii_lowercase)}
    N = int(input())
    ans = []

    def dfs(s, mx):
        if len(s) == N:
            print(s)
            return
        else:
            for i in range(mx+2):
                v = s + dic[i]
                mx = max(mx, i)
                dfs(v, mx)
    dfs("a", 0)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    from string import ascii_lowercase
    dic = {s: i for i, s in enumerate(ascii_lowercase)}
    rdic = {i: s for i, s in enumerate(ascii_lowercase)}
    N = int(input())
    ans = []

    def dfs(n, s):
        if N < n:
            return
        elif n == N:
            ans.append(s)
            return
        else:
            for i in range(max([dic[c] for c in s])+2):
                v = s + rdic[i]
                dfs(n+1, v)
    dfs(1, "a")
    ans.sort()
    print(*ans, sep="\n")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(read())


def check(s):
    if len(s) == n:
        print(s)
    else:
        for i in range(97, ord(max(s)) + 2):
            check(s + chr(i))


check('a')

Submission status: Accepted

Solution code:
N = int(input())

def dfs(s, mx):
    if len(s) == N:
        print(s)
    else:
        c = 'a'
        while ord(c) <= ord(mx):
            if c == mx:
                dfs(s + c, chr(ord(mx) + 1))
            else:
                dfs(s + c, mx)
            c = chr(ord(c) + 1)

dfs('', 'a')

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())

alpha=['a','b','c','d','e','f','g','h','i','j','k']

q=deque(['a'])

for i in range(n-1):
    qi_ans=[]
    while(len(q)>0):
        qi=q.popleft()
        qiword_maxind=0
        for j in range(len(qi)):
            qi_ans.append(qi+qi[j])
            qij_ind=alpha.index(qi[j])
            if(qiword_maxind<qij_ind):
                qiword_maxind=qij_ind
        else:
            qi_ans.append(qi+alpha[qiword_maxind+1])
    qi_ans=sorted(list(set(qi_ans)))
#    print(qi_ans)
    q.extend(qi_ans)

lenq=len(q)
for i in range(lenq):
    print(q.popleft())

Submission status: Accepted

Solution code:
N = int(input())

alp = {"a":1, "b":2, "c":3, "d":4, "e":5, "f":6, "g":7, "h":8, "i":9, "j":10}
alp2 = "abcdefghij"

def solve(N):
    ans = []
    if N == 1:
        ans.append("a")
    else:
        pre = solve(N-1)
        for i in range(len(pre)):
            tmp = sorted(pre[i])
            num = alp[tmp[len(tmp)-1]]
            for j in range(num+1):
                ans.append(pre[i]+alp2[j])
    return ans
            

f_ans = solve(N)
f_ans.sort()

for i in range(len(f_ans)):
    print(f_ans[i])

Submission status: Accepted

Solution code:
N = int(input())

ss = ['a']
for i in range(1, N):
    tmp = []
    # length i+1
    for s in ss:
        newc = ord(max(s)) + 1
        for j in range(ord('a'), newc+1):
            tmp.append(s + chr(j))
    ss = tmp

for s in sorted(ss):
    print(s)

Submission status: Accepted

Solution code:
N=int(input())
def dfs(S):
    if len(S)==N:
        print(S)
    else:
        for i in range(97,ord(max(S))+2):
            dfs(S+chr(i))
dfs("a")

Submission status: Accepted

Solution code:
N=int(input())
A=["a"]
S="abcdefghijklmn"
for i in range(N-1):
    B=[]
    for a in A:
        for s in S[:len(set(a))+1]:
            B.append(a+s)
    A=B[::]
print("\n".join(A))

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = "abcdefghijk"

# q = [("", -1)]
ans = 0

def dfs(a, mx):
    # q = [(cur, mx)]
    # while q:
    # a, mx = q.pop()
    if len(a) == N:
        print(a)
        return
    for c in range(len(S[:mx+2])):
        t = a
        t += S[:mx+2][c]
        dfs(t, max(c, mx))

dfs("", -1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N=int(input())

l0=['a']
l1=[]
li=[0]
li1=[]
alp=[chr(i) for i in range(97, 97+26)]

for i in range(1,N):
    for j, l in enumerate(l0):
        ind=0
        while ind<=li[j]+1:
            l1.append(l+alp[ind])
            if ind==li[j]+1: li1.append(li[j]+1)
            else: li1.append(li[j])
            ind+=1
    l0=l1.copy()
    l1=[]
    li=li1.copy()
    li1=[]

for l in l0:
    print(l)

Submission status: Accepted

Solution code:
N = int(input())

def dfs(s, mx):
    if (len(s) == N):
        print(s)
        return
    for i in range(mx + 1):
        dfs(s + chr(97 + i), mx + (i == mx))

dfs('', 0)

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
d = {i:[] for i in range(1, N+1)}
q = deque([(1, "a")])

while q:
    pos, s = q.popleft()
    if pos == N+1:
        break
    d[pos].append(s)
    size = len(set(s))
    for i in range(size+1):
        q.append((pos+1, s+chr(97+i)))

for i in d[N]:
    print(i)

Submission status: Accepted

Solution code:
from collections import deque

d = {i:[] for i in range(1, 11)}
q = deque([(1, "a")])
while q:
    pos, s = q.popleft()
    if pos == 11:
        break
    d[pos].append(s)
    size = len(set(s))
    for i in range(size+1):
        q.append((pos+1, s+chr(97+i)))

N = int(input())
for i in d[N]:
    print(i)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5)

n = int(input())
ans = []

def dfs(arr, max_val):
    if len(arr) == n:
        ans.append(arr)
        return
    for i in range(max_val + 2):
        next_arr = arr[:]
        next_arr.append(i)
        dfs(next_arr, max(max_val, i))

dfs([0], 0)

ans.sort()
orda = ord("a")
for line in ans:
    print("".join([chr(orda + item) for item in line]))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
from sys import stdin
import numpy as np
#import math
#from functools import reduce
    
#N, M = [int(x) for x in stdin.readline().rstrip().split()]
N = int(input())
#S = list(input())

def append_str(W, N):
    w_new = []
    for w in W:
        kind = len(set(w))
        for i in range(kind+1):
            w_new.append(w+alphabets[i])
    return w_new
    

seed = 'a'
alphabets = ['a', 'b','c','d','e','f','g','h','i','j']

#長さNの文字列に含まれる文字の種類はN以下
for n in range(N):
    if n == 0:
        w = [seed]
    else:
        w = append_str(w, n+1)
    
for buf in w:
    print(buf)

Submission status: Accepted

Solution code:
N=int(input())
A=["a","b","c","d","e","f","g","h","i","j"]
L=[{}for i in range(N+1)]
L[1]["a"]=1
#print(L)

for i in range(2,N+1):
    for j,k in L[i-1].items():
        for l in range(k+1):
            L[i][j+A[l]]=max(l+1,k)
#print(L[N])
ans=L[N].keys()
ans=list(ans)
ans.sort()
for i in range(len(ans)):
    print(ans[i])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)

N = 0


def dfs(S, M):
    if len(S) == N:
        print(S)
        return
    for c in range(ord('a'), M + 1):
        dfs(S + chr(c), max(M, c + 1))


def solve():
    global N
    N = Scanner.int()
    dfs('', ord('a'))


def main():
    # sys.stdin = open("sample.txt")
    solve()


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# カンニングしました。。。。。。。。

def dfs(s: str, m: int):
    global n
    if len(s) == n:
        print(s)
    else:
        for c in range(ord('a'), m + 1):
            dfs(s + chr(c), m + 1 if c == m else m)


n = int(input())
dfs('', ord('a'))

Submission status: Accepted

Solution code:
n = int(input())

def dfs(s,mx):
    if len(s)==n:
        print(s)
        return None
    for i in range(ord('a'),mx+1+1):
        dfs(s+chr(i),max(mx,i))
dfs('',ord('a')-1)

Submission status: Accepted

Solution code:
def f(num,N,S):
    alphabet = ["a","b","c","d","e","f","g","h","i","j"]
    if N == num:
        for i in S:
          print(i)
        return
    else:
        tmp = []
        length = len(S)
        for i in S:
            ttmp = sorted(list(i))
            for j in range(alphabet.index(ttmp[-1])+2):
                tmp.append(i+alphabet[j])
        f(num+1,N,tmp)
 
N = int(input())
f(1,N,["a"])

Submission status: Accepted

Solution code:
def f(num,N,S):
    alphabet = ["a","b","c","d","e","f","g","h","i","j"]
    if N == num:
        for i in range(len(S)):
            print(S[i])
        return
    else:
        tmp = []
        length = len(S)
        for i in S:
            ttmp = sorted(list(i))
            for j in range(alphabet.index(ttmp[-1])+2):
                tmp.append(i+alphabet[j])
        f(num+1,N,tmp)
 
N = int(input())
f(1,N,["a"])

Submission status: Accepted

Solution code:
from collections import deque

def dfs(q):
    if len(q)==N:
        print(''.join(q))
        return
    
    for i in range(min(26, ord(max(q))-ord('a')+2)):
        q.append(alpha[i])
        dfs(q)
        q.pop()
    
N = int(input())
alpha = 'abcdefghijklmnopqrstuvwxyz'
dfs(deque(['a']))

Submission status: Accepted

Solution code:
# D - String Equivalence 
def dfs(s, mx):
    if len(s)==n:
        print(s)
        return
    for c in range(ord('a'),ord(mx)+1):
        dfs(s+chr(c),(chr(ord(mx)+1) if ord(mx)==c else mx))

n=int(input())
dfs('','a')

Submission status: Accepted

Solution code:
n=int(input())
ans=[]

def dfs(i,c,d):
	if i==n:
		ans.append(c)
		return
	for j in range(0,d+1):
		dfs(i+1,c+chr(97+j),d+(j==d))

dfs(0,"",0)

print("\n".join(ans))

Submission status: Accepted

Solution code:
n = int(input())

s = ['a']*(n)
#print(''.join(s))

def dfs(x,alp):
    if x == n:
        print(''.join(s))
        return
    
    for i in range(alp+1):
        #print(x,alp,i)
        s[x] = chr(i+ord("a"))
        dfs(x+1,max(i+1,alp))
        #print(alp)


dfs(0,0)

Submission status: Accepted

Solution code:
from itertools import combinations
def g(i):
    return chr(i+96)
def f(n,i):
    global Ind
    if n==0:
        B.append("".join(A))
        return
    if n==1:
        j = Ind.pop()
        A[j] = g(i)
        B.append("".join(A))
        A[j]=0
        Ind.append(j)
        Ind = sorted(Ind)
        return
    j0 = Ind.pop(0)
    Ind1 = Ind[:]
    A[j0] = g(i)
    for k in range(n):
        for x in combinations(Ind1,n-1-k):
            tmp = []
            for j in range(n-1-k):
                A[x[j]] = g(i)
                tmp.append(x[j])
            for j in tmp:
                Ind.remove(j)
            f(k,i+1)
            for j in tmp:
                A[j] = 0
                Ind.append(j)
            Ind = sorted(Ind)
    Ind.append(j0)
    Ind = sorted(Ind)
    
N = int(input())
A = [0 for _ in range(N)]
Ind = list(range(N))
B = []
f(N,1)
B = sorted(B)
for b in B:
    print(b)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import deque

N = int(input())

chars = "abcdefghij"

queue = deque()
queue.append(('a', 1))

ans = []

while queue:
    s, num = queue.popleft()
    if len(s) == N:
        print(s)
    else:
        for i in range(num+1):
            ss = s + chars[i]
            if i == num:
                queue.append((ss, num+1))
            else:
                queue.append((ss, num))

Submission status: Accepted

Solution code:
n = int(input())

ansl = []
def dfs(s, max_s):
    if len(s) == n:
        ansl.append(s)
        return
    ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
    for i in range(max_s+1):
        dfs(s+ascii_lowercase[i], max(i+1, max_s))

dfs("", 0)
for a in ansl:
    print(a)

Submission status: Accepted

Solution code:
"""
DFSを実装する.再帰を計算する必要があるので，関数の定義が必要
a - a -a
      -b
  - b -a
      -b
      -c
"""
N=int(input())
a_uni = ord('a')
z_uni = ord('z')
def dfs(s,maximum_uni):
    if len(s)==N:
        print(s)
        return None
    for i in range(a_uni,(maximum_uni+1)+1):
        maximum_uni = max(maximum_uni,i)
        dfs(s+chr(i),maximum_uni)
dfs('a',a_uni)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())

arrays = list("abcdefghij")
pat_dic = {i:arrays[i] for i in range(N)}
par_patterns = list(range(10))

patterns = par_patterns[:N]
def next_search(arr,N):
    tmp = arr.copy()
    if len(tmp) == N:
        print(''.join([pat_dic[tmp[i]] for i in range(N)]))
        return None
    for st in patterns:
        if max([0]+tmp)+1 < st:
            continue
        tmp.append(st)
        if tmp[0] != 0:
            continue

        next_search(tmp, N)
        tmp = arr.copy()

next_search([], N)

Submission status: Accepted

Solution code:
N = int(input())
seed = "a"
alpha = "abcdefghij"


def dfs(string, mx_ind):
    if len(string) == N:
        print(string)
    else:
        for i in range(mx_ind + 1):
            dfs(string + alpha[i], mx_ind + 1 if i == mx_ind else mx_ind)


dfs("", 0)

Submission status: Accepted

Solution code:
N=int(input())

l=["a"]

a=ord("a")

for i in range(N-1):
    la=[]
    for j in l:
        b=ord(max(j))
        for k in range(a,b+2):
            la.append(j+chr(k))
    l=la

for i in l:
    print(i)

Submission status: Accepted

Solution code:
n=int(input());s=['a']
while len(s)>0:
  t=s.pop(-1)
  if len(t)==n:print(t)
  else:
    a=max(ord(i)for i in t)
    for a in range(a+1,96,-1):s.append(t+chr(a))

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())

num_to_alp = {0:'a', 1:'b', 2:'c', 3:'d', 4:'e', 5:'f', 6:'g', 7:'h', 8:'i', 9:'j'}
alp_to_num = {'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5, 'g':6, 'h':7, 'i':8, 'j':9}
words = deque([[0]])

def next_letters(x):
    letters = deque([])
    for i in range(x + 2):
        letters.append(i)
    return letters

while len(words[0]) < n:
    word = words.popleft()
    letters = next_letters(max(word))
    for l in letters:
        l = [l]
        words.append(word + l)

for word in words:
    word = [num_to_alp[i] for i in word]
    print(''.join(word))

Submission status: Accepted

Solution code:
N = int(input())
ans_list = ["a"]  # 生成元の初期状態 (N = 1 の答え)
alphabet = [chr(ord("a") + i) for i in range(N)]  # 使用するアルファベット

for i in range(N - 1):
    ans_list = [ans + x for ans in ans_list for x in alphabet[:len(set(ans)) + 1]]

for ans in ans_list:
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
import copy
ans = [[0]]
for i in range(n-1):
    x = len(ans)
    for j in range(x):
        mx = max(ans[j])
        for k in range(mx+2):
            tmp = ans[j].copy()
            tmp.append(k)
            ans.append(tmp)
    ans = [s for s in ans if len(s)==i+2]
for s in ans:
    tmp = ""
    for i in s:
        tmp += chr(ord("a")+ i)
    print(tmp)

Submission status: Accepted

Solution code:
N = int(input())
# 最後の数字+1以下じゃないとだめ
# abc -> 次はa,b,c,d

dic = ["a","b","c","d","e","f","g","h","i","j"]

strs = ["a"]
cnt = 1
while cnt < N:
    tmps = []
    for s in strs:
        for i,d in enumerate(dic[::-1]):
            if d in s:
                ind = len(dic)-i-1
                break
        # print(s,ind,dic[i])
        for i in range(ind+2):
            tmps.append(s+dic[i])
    cnt += 1
    strs = tmps[:]
for s in strs:
    print(s)

Submission status: Accepted

Solution code:
str = 'abcdefghijklmnop'
def bt(n,m,s):
  if n == 0:
    print(s)
    return
  for c in range(m):
    bt(n-1,m,s+str[c])
  #introduce another character
  bt(n-1,m+1,s+str[m])
n = int(input())
bt(n,0,'')

Submission status: Accepted

Solution code:
n = int(input())
a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

def dfs(s, count):
    if len(s) == n:
        print(s)
        return

    for z in range(count + 1):
        dfs(s + a[z], count + 1 if z == count else count)


dfs("", 0)

Submission status: Accepted

Solution code:
def examA():
    L = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
    K = I()

    ans = L[K-1]
    print(ans)
    return

def examB():
    H, W = LI()
    if H==1 or W==1:
        print(1)
        return
    ans = H*W//2 + (H*W)%2
    print(ans)
    return

def examC():
    a,b,c = LI()
    if c-(a+b)<=0:
        print("No")
        return
    judge = (c-a-b)**2-4*a*b
    if judge>0:
        print("Yes")
    else:
        print("No")
    return

def examD():
    def bfs(N):
        W = [0]*(5*10**6)
        L = [""]*(5*10**6)
        que = deque()
        que.append(0)
        L[0] = "a"
        W[0] = 0
        flag = True
        ne = 0
        while(flag):
            now = que.popleft()
            nowW = W[now]
            if len(L[now])==N:
                flag = False
                break
            for i in range(W[now]+2):
                ne += 1
                W[ne] = max(nowW,i)
                que.append(ne)
                L[ne] = L[now] + alphabet[i]
        return L
    N = I()
    D = bfs(N)
    for ans in D:
        if not ans:
            break
        if len(ans)<N:
            continue
        print(ans)
    return

def examD2():
    N = I()
    def dfs(n,s,i):
        if len(s)==n:
            return
        for k in range(i+1):
            if len(s)==n-1:
                print(s+alphabet[k])
            dfs(n,s+alphabet[k],max(i,k+1))
        return
    dfs(N,"",0)
    return

def examE():
    def LCS_R(S,T):
        N = len(S); M = len(T)
        dp = [[inf] * (M + 1) for _ in range(N + 1)]
        dp_S = [[""] * (M + 1) for _ in range(N + 1)]
        dp[0][0] = 0
        for i in range(N):
            for j in range(M):
                if S[i] == T[j] or S[i] == "?" or T[j] == "?":
                    dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + 1)
                    if dp[i + 1][j + 1]==dp[i][j] + 1:
                        cur = S[i]
                        if S[i] == "?" or T[j] == "?":
                            cur = "?"
                        dp_S[i+1][j+1] = dp_S[i][j] + cur
                dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]+1)
                dp[i][j+1] = min(dp[i][j+1], dp[i][j] + 1)
                if dp[i + 1][j] == dp[i][j] + 1:
                    dp_S[i + 1][j] = dp_S[i][j] + S[i]
                if dp[i][j + 1] == dp[i][j] + 1:
                    dp_S[i][j + 1] = dp_S[i][j] + T[j]
        dp[N][M] = min(dp[N][M], dp[N-1][M] + 1)
        dp[N][M] = min(dp[N][M], dp[N][M-1] + 1)
        if dp[N][M] == dp[N-1][M] + 1:
            dp_S[N][M] = dp_S[N-1][M] + S[N-1]
        if dp[N][M] == dp[N][M-1] + 1:
            dp_S[N][M] = dp_S[N][M-1] + T[M-1]

        i = N; j = M
        rep = []
        while i > 0 and j > 0:
            if dp[i][j] == dp[i - 1][j]:
                rep.append(S[i-1])
                i -= 1
            elif dp[i][j] == dp[i][j - 1]:
                rep.append(T[j-1])
                j -= 1
            else:
                rep.append(S[i - 1])
                i -= 1
                j -= 1

        return dp,dp_S

    A = SI()
    B = SI()
    C = SI()

    a1,d1 = LCS_R(A,B)
    a2,d2 = LCS_R(B,C)
    a3,d3 = LCS_R(C,A)


    S1,d1 = LCS_R(d1[-1][-1],C)
    S2,d2 = LCS_R(d2[-1][-1],A)
    S3,d3 = LCS_R(d3[-1][-1],B)

    ans = min(S1[-1][-1],S2[-1][-1],S3[-1][-1])
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examD2()

"""

"""

Submission status: Accepted

Solution code:
n = int(input())
alphabets = 'abcdefghijkl'
norms = [['a']]
for i in range(1,n):
  rtn = []
  for norm in norms[-1]:
    n_ch = len(set(norm))
    for j in range(n_ch+1):
      rtn.append(norm+alphabets[j])
  norms.append(rtn)
  
for norm in norms[-1]:
  print(norm)

Submission status: Accepted

Solution code:
N = int(input())
S = "abcdefghijklmn"
Q = [(1,1,"a")]
A = []
while Q:
    q = Q.pop(-1)
    if q[0]==N:
        A.append(q[2])
        continue
    for i in range(q[1]+1):
        Q.append((q[0]+1,max(q[1],i+1),q[2]+S[i]))
for a in sorted(A):print(a)

Submission status: Accepted

Solution code:
n=int(input())
def make(floor,kind,name): #floor=何階層目か,kind=何種類使ってるか
    if floor==n+1: 
        print(name)
        return
    num=min(floor,kind+1)
    for i in range(num):
        use=0 #新しい文字使ってるか
        if kind-1<i:
            use=1
        make(floor+1,kind+use,name+chr(i+97))
make(1,0,"")

Submission status: Accepted

Solution code:
n = int(input())

al = ['a','b','c','d','e','f','g','h','i','j']
w = ['a']

for num in range(n-1):
    x = []
    for i in range(len(w)):
        x.append(w[i] + 'a')
        for num in range(num+1):
            if al[num] in w[i]:
                x.append(w[i] + al[num+1])
    w = x

for _ in range(len(w)):
    print(w[_])

Submission status: Accepted

Solution code:
N = int(input())


def f(n, lst):
    if n == N:
        print(''.join(map(str, [chr(s + 97) for s in lst])))
    else:
        for i in range(max(lst) + 2):
            f(n + 1, lst + [i])


f(1, [0])

Submission status: Accepted

Solution code:
n = int(input())

tank = []
tank.append(['a'])
for i in range(n):
    tmp_tank = []
    for w in tank[i]:
        tmp_max = max(list(w))
        nxt_max = chr(ord(tmp_max)+1)
        for i in range(ord('a'), ord(nxt_max)+1):
            tmp_tank.append(w+chr(i))
    tank.append(tmp_tank)

for w in tank[n-1]:
    print(w)

Submission status: Accepted

Solution code:
def dfs(a='', b='a'):
	if len(a) == N:
		print(a)
	else:
		i = 'a'
		while i < b:
			dfs(a + i, b)
			i = chr(ord(i) + 1)
		dfs(a + b, chr(ord(b) + 1))

N = int(input())
dfs()

Submission status: Accepted

Solution code:
n = int(input())

before = ["0"]
for i in range(n-1):
    next = []
    for elem in before:
        m = 1
        for j in range(len(elem)):
            m = max(m, int(elem[j])+1)
        for j in range(m+1):
            next.append(elem+str(j))
    before = next[:]

before.sort()
d = {"0":"a", "1":"b", "2":"c", "3":"d", "4":"e", "5":"f", "6":"g", "7":"h", "8":"i", "9":"j"}
for elem in before:
    ans = []
    for i in range(len(elem)):
        ans.append(d[elem[i]])
    print("".join(ans))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
n = int(input())

def rec(s, mx, l):
    if l >= n:
        print("".join([chr(ord("a") + x) for x in s]))
        return
    for x in range(mx+2):
        rec(s+[x], max(mx, x), l+1)

rec([0], 0, 1)

Submission status: Accepted

Solution code:
def dfs(s, mx):
    if len(s) == n:
        print(s)
        return
    for i in range(ord('a'), mx + 2):
        dfs(s + chr(i), max(mx, i))


n = int(input())
dfs('', ord('a') - 1)

Submission status: Accepted

Solution code:
n = int(input())
alphabet = list("abcdefghij"[:n])

# DFS
def print_string(string="", depth=1):
    if len(string) == n:
        print(string)
    else:
        for i in range(depth):
            if i+1 == depth:
                print_string(string+alphabet[i], depth=depth+1)
            else:
                print_string(string+alphabet[i], depth=depth)
                
print_string()

Submission status: Accepted

Solution code:
N = int(input())

def dfs(s, mx):
  if len(s) == N:
    print(s)
  else:
    for i in range(ord('a'), ord(mx)+1):
      dfs(s+chr(i), chr(ord(mx)+1) if chr(i) == mx else mx)
      
dfs('', 'a')

Submission status: Accepted

Solution code:
N = int( input() )
alphabets = "abcdefghij"[ :N ]
types= ["a"]

for _ in range(N-1):
  for j in range(len(types)):
    target=types.pop(0)
    #print(set(target))
    for k in range(len(set(target))+1):
      types.append(target+alphabets[k])
    #print(types)
types.sort()
for each in types:
  print(each)

Submission status: Accepted

Solution code:
n = int(input())
dic_a = {str(k):v for k,v in zip(range(10),'abcdefghij')}

def main(s):
    if len(s) < n:
        for ss in range(int(max(s))+2):
            main(s+str(ss))
    else:
        print(''.join([dic_a[x] for x in s]))
            
main('0')

Submission status: Accepted

Solution code:
def main():
    number = int(input())
    queue = ["a"]
    for i in range(number - 1):
        now_queue = []
        for now in queue:
            limit = ord(max(now)) + 2
            for j in range(ord("a"), limit):
                now_queue.append(now + chr(j))
        queue = now_queue
    for q in queue:
        print(q)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
def dfs(s):
    if len(s)==n:print(s)
    else:
        for i in range(97,ord(max(s))+1+1):
            dfs(s+chr(i))
dfs("a")

Submission status: Accepted

Solution code:
alphabetlist=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

N=int(input())
S=[0 for i in range(0,N)]
string=''
for i in range(0,N):
    string+='a'
print(string)
digit=N-1
while True:
    if digit!=0:
        if S[digit]==max(S[0:digit])+1:
            digit-=1
        else:
            S[digit]+=1
            for j in range(digit+1,N):
                S[j]=0
            digit=N-1
            if S[0]==1:
                break
            string=[0 for i in range(0,N)]
            for i in range(0,N):
                string[i]=alphabetlist[S[i]]
            print(''.join(string))
    else:
        break

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
def dfs(s):
    if len(s)==n:
        print(s)
    else:
        for i in range(ord("a"),ord(max(s))+1+1): #"a"スタートで、max(s)+1("a"の分)+1(右端)
            dfs(s+chr(i))
dfs("a")