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:
N = int(input())

All = []
def enumeration(a="0", b = 1):
    if len(a) == N:
        All.append(a)
    else:
        for i in range(len(set(a)) + 1):
            enumeration(a + str(i), b+1)

enumeration()
for i in range(len(All)):
    All[i] = list(All[i])
    for p in range(len(All[i])):
        All[i][p] = chr(97 + int(All[i][p]))
    print("".join(All[i]))

Submission status: Accepted

Solution code:
import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def dfs(s, mx, n):
    l = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
    if len(s) == n:
        print(s)
        return
    for i in range(mx+2):
        t = s
        t += l[i]
        dfs(t, max(mx, i), n)


def main():
    n = int(input())
    dfs('', -1, n)


main()

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 

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


def solve(n, s, mx):
    if len(s) == n:
        return [s]
    result = []
    for co in range(ord('a'), mx+2):
        c = chr(co)
        mx = max(mx, co)
        r = solve(n, s+c, mx)
        result.extend(r)
    return result

r = solve(load(), 'a', ord('a'))

for e in r:
    print(e)

Submission status: Accepted

Solution code:
n = int(input())
alphabet = 'abcdefghij'

def ans(n):
    if n == 1:
        return ['a']
    ret = []
    for s in ans(n-1):
        l = len(set(s))
        for i in range(l+1):
            ret.append(s + alphabet[i])
    return ret

for s in ans(n):
    print(s)

Submission status: Accepted

Solution code:
N = int(input())

ans = ['a']
for i in range(N-1):
    tmp = []
    for s in ans:
        m = max(s)
        j = ord(m) - ord('a') + 1
        for k in range(j+1):
            tmp.append(s + chr(ord('a') + k))
    ans = tmp
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
ans = []
sets = {}
def dfs(s,keta,mx):
  if keta == n+1:
    ans.append(s)
  else:
    for i in range(mx):
      dfs(s+chr(ord("a")+i),keta+1,max(i+2,mx))

dfs("",1,1)
ans.sort()
print(*ans,sep = "\n")

# data10 = ["aaaaaaaaaa","abcdefghij"]

Submission status: Accepted

Solution code:
A = 'a'
S = 'abcdefghij'
for _ in range(int(input())-1):
    A = [a + s for a in A for s in S[:len(set(a)) + 1]]
print('\n'.join(A))

Submission status: Accepted

Solution code:
n = int(input())
str_list = [chr(ord('a') + i) for i in range(26)]


def dfs(str_cace):
    if len(str_cace) == n:
        print(str_cace)
        return
    else:
        max_index = 0
        for i in range(len(str_cace)):
            max_index = max(max_index, str_list.index(str_cace[i]))
        max_index += 1

        for cad_str in str_list:
            if str_list.index(cad_str) <= max_index:
                dfs(str_cace+cad_str)


dfs("a")

Submission status: Accepted

Solution code:
tt=[]
ss=[[]]
nn=[]
def ylist(n,nn,ss,s,nstate,tt):
    if n<=1:
        tt.append(s)
        return 
    n=n-1
    for i in range(0,nstate):
        s1=s+[nn[i]]
        ss.append(s1)
        ylist(n,nn,ss,s1,nstate,tt)
    nstate=nstate+1
    s1=s+[nn[nstate-1]]
    ss.append(s1)
    ylist(n,nn,ss,s1,nstate,tt)
    return

n=int(input())  
#n=4

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

nstate=1    
s=["a"]
ss=[]
tt=[]
ylist(n, nn, ss, s, nstate,tt)
for t in tt:
    t2=""
    for ts in t  :
        t2=t2+ts
    print(t2)

Submission status: Accepted

Solution code:
def f(n, count=0, i=0, s=""):
    global ans
    if count == n:
        ans.append(s)
        return
    if i == 0:
        f(n, count + 1, i=i+1, s=s+"a")
    else:
        for j in range(i + 1):
            f(n, count + 1, i=max(i, j + 1), s=s+chr(ord("a") + j))

import sys
sys.setrecursionlimit(10000)
n = int(input())
ans = []
f(n)
ans.sort()
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
N = int(input())


def DFS(N, S):
    if len(S) == N:
        print(S)
    else:
        if S:
            next_max_chr = ord(max(S)) + 1
        else:
            next_max_chr = ord("a")
        for i in range(0, next_max_chr - ord("a") + 1):
            DFS(N, S + chr(ord("a") + i))


DFS(N, "")

Submission status: Accepted

Solution code:
N = int(input())


def DFS(N, S):
    if len(S) == N:
        print(S)
    else:
        next_chrs = []
        if S:
            next_max_chr = ord(max(S)) + 1
        else:
            next_max_chr = ord("a")
        for i in range(0, next_max_chr - ord("a") + 1):
            DFS(N, "".join([S, chr(ord("a") + i)]))


DFS(N, "")

Submission status: Accepted

Solution code:
def main():
  a='a'
  for _ in~-int(input())*a:
    a=(s+chr(c)for s in a for c in range(97,ord(max(s))+2))
  print('\n'.join(a))
main()

Submission status: Accepted

Solution code:
def main():
  a='a'
  for _ in~-int(input())*a:
    a=[s+chr(c)for s in a for c in range(97,ord(max(s))+2)]
  print('\n'.join(a))
main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)

n = int(input())

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


mx = ord('a')
dfs('a', mx)

Submission status: Accepted

Solution code:
N = int(input())

answer = [(97, 'a')]
for _ in range(N - 1):
    new_answer = []
    for i, j in answer:
        for k in range(97, i + 1):
            new_answer.append((i, j + chr(k)))
        new_answer.append((i+1,j+chr(i+1)))
    answer = new_answer[:]

answer = [j for _, j in answer]
answer.sort()
print('\n'.join(answer))

Submission status: Accepted

Solution code:
def f(n, count=0, i=0, lst=[]):
    global ans
    if count == n:
        s = ""
        for j in range(count):
            s += chr(ord("a") + lst[j])
        ans.append(s)
        return
    if i == 0:
        lst.append(i)
        f(n, count + 1, i=i+1, lst=lst)
    else:
        for j in range(i + 1):
            f(n, count + 1, i=max(i, j + 1), lst=lst+[j])

import sys
sys.setrecursionlimit(10000)
n = int(input())
ans = []
f(n)
ans.sort()
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**5)
def dfs(s, mx):
  if len(s)==N:
    print(s)
  else:
    for i in range(mx+1):
      if i<mx:
        dfs(s+chr(ord('a')+i),mx)
      else:
        dfs(s+chr(ord('a')+i), mx+1)

N = int(input())
dfs("",0)

Submission status: Accepted

Solution code:
N = int(input())
X = [chr(ord('a') + i) for i in range(N)]
first_presence = {x: i for x, i in zip(X, list(range(N)))}

R = ['a']
for i in range(1, N):
    next_r = []
    for r in R:
        max_s = max(r)
        for x in X[:first_presence[max_s] + 2]:
            next_r.append(r + x)
    R = next_r

ans = R
ans.sort()
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
N = int(input())
cur = [('a', 0)]
for _ in range(N-1):
    new = []
    for s, c in cur:
        for i in range(c + 2):
            new.append((s + chr(ord('a') + i), max(i, c)))
    cur = new
for s, c in cur:
    print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000)

def dfs(s, mx):
    if len(s) == N:
        print(s)
        return 0
    for c in range(ord("a"), mx + 2):
        t = s
        t += chr(c)
        dfs(t, max(mx, c))

N = int(input())
dfs("", ord("a") - 1)

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()

def dfs(s, limit):
    if len(s) == N:
        print(s)
    else:
        for i in range(limit+2):
            x = chr(i + ord('a'))
            dfs(s + x, max(limit, i))

dfs('a', 0)
# 23

Submission status: Accepted

Solution code:
n=int(input())
dic={0:'a',1:'b',2:'c',3:'d',4:'e',5:'f',6:'g',7:'h',8:'i',9:'j'} 
#標準形の文字列は同型の文字列の中で辞書順最小であることから、新たに付け加えるべき文字は順にa,b,c...となる
ans=[['a',1]]
for i in range(1,n):
 tmp=[]
 for prev,val in ans:
   for j in range(val+1): 
    #(今見ている文字列に使われている文字の種類数+1)種類の文字を付け加える
     if j!=val: 
        #今見ている文字列に使われている文字を付け加えるとき、文字の種類数は増加しない
       tmp.append([prev+dic[j],val])
     else: 
      #今見ている文字列に使われていない文字を付け加えるとき、文字の種類数が増加する
       tmp.append([prev+dic[j],val+1])
 ans=tmp
ans=sorted(ans,key=lambda x:x[0]) 
#生成した標準形の文字列を辞書順昇順に並べ替える
for i in range(len(ans)):
 print(ans[i][0])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:


def dfs(string,n):
    if len(string)==n:
        print(string)
    else:
        s=string
        for i in range(97,max([ord(j)for j in string])+2):
            dfs(string+chr(i),n)
        

dfs("a",int(input()))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)

n = int(input())

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


mx = ord('a')
dfs('a', mx)

Submission status: Accepted

Solution code:
N = int(input())
ans_list = ["a"]
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())

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

scripts = [[] for i in range(N)]
scripts[0] = ["a"]

for i in range(N-1) :
  for ii in range(N-1) :
    if(len(scripts[N-ii-1]) > 0) :
      scripts[N-ii-1] = [str1+str2 for str1 in scripts[N-ii-1] for str2 in alphabets[0:N-ii]]
    scripts[N-ii-1].extend([str1+alphabets[N-ii-1] for str1 in scripts[N-ii-2]])
  scripts[0] = [scripts[0][0]+alphabets[0]]
  
scripts_flatten = []
for i in range(N) :
  scripts_flatten.extend(scripts[i])
scripts_flatten.sort()
for string in scripts_flatten :
  print(string)

Submission status: Accepted

Solution code:
n = int(input())

q = ['a']
for i in range(n-1):
    nq=[]
    for j in q:
        stop=ord(max(j))+2

        for k in range(ord('a'),stop):
            nq.append(j+chr(k))
        q=nq

for i in q:
    print(i)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()

S = [[0]]
ans = []
while S:
    v = S.pop()
    if len(v) == N:
        ans.append(v)
        continue
    else:
        for u in range(max(v)+2):
            S.append(v+[u])

for i,a in enumerate(ans):
    a = list(map(lambda x: chr(x+ord('a')),a))
    ans[i] = ''.join(a)

ans.sort()

for a in ans:
    print(a)

Submission status: Accepted

Solution code:
def f(word,n):
    if len(word)==N:return print(word)
    else:
        for i in range(n+1):
            f(word+chr(97+i),n+1 if i==n else n)


N=int(input())
f("",0)

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = [[0]]
limit = 0
for i in range(N-1):
    B = []
    for j in range(limit + 2):
        for x in A:
            if max(x) + 1 < j:
                continue
            y = x + [j]
            B.append(y)
    A = B
    limit += 1

A = [tuple(x) for x in A]
A = sorted(set(A))
for x in A:
    x = [chr(y + ord('a')) for y in x]
    print(''.join(x))

Submission status: Accepted

Solution code:
N = int(input())
def DFS(s, mx):
    if len(s) == N:
        print(s)
        return
    c = 'a'
    while c <= chr(ord(mx)+1):
        t = s + c
        DFS(t, chr(max(ord(c), ord(mx))))
        c = chr(ord(c) + 1)
DFS("", chr(ord('a')-1))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
def f(s, last):
    if len(s) == n:
        print(s)
    else:
        for i in range(97, last + 2):
            f(s + chr(i), last + 1 if i == last + 1 else last)


f('', 96)

Submission status: Accepted

Solution code:
n = int(input())
a = ord('a')

def dfs(s, m):
    # print("dfs", s, chr(m))
    if len(s) == n:
        print(s)
    else:
        for i in range(m + 1 - a):
            dfs(s + chr(a + i), m + 1 if i == m - a else m)

dfs("", a)

Submission status: Accepted

Solution code:
from collections import deque, defaultdict, Counter
from math import ceil, floor
from string import ascii_lowercase as AL
import sys
def input():
    return sys.stdin.readline().strip()


# N
# 1 a
# 2 aa, ab
# 3 aaa, aab, aba, abb, abc

def dfs(s, mx, N):
    # print("call |{}|{}|{}|{}|{}|".format(s, mx, N, len(s), len(s) == N))
    if len(s) == N:
        print(s)
    else:
        for ch in AL:
            if ord(ch) <= ord(mx):
                next_ch = chr(ord(ch) + 1) if ch == mx else mx
                dfs(s + ch, next_ch, N)

N = int(input())
dfs("", 'a', N)

# print(AL)
# for c in AL:
#     print(c, ord(c))

Submission status: Accepted

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

def dfs( s , mx ):
    if len(s) == n:
        ans.append(s)
        return
    else:
        for j in range(mx+1):
            t = chr(j + 97)
            mx2 = ord(max(s+t)) - 96
            dfs( s+t , mx2 )
            
dfs('a' , 1)

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
from collections import deque

q = deque()

q.append('a')

n = int(input())

ans_list = []
flag = 0

if n != 1:
    while flag == 0:
        s = q.pop()
        if len(s) > n:
            flag = 1
        a = list(s)
        x = chr(ord('a')-1)
        a = map(ord,a)
        max_a = max(a)
        while ord(x) != max_a+1:
            x = chr(ord(x)+1)
            y = s + x
            q.appendleft(y)
            if len(y) == n:
                ans_list.append(y)
else:
    ans_list = ['a']

ans_list = sorted(ans_list)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

A = "abcdefghij"
N = 0
def dfs(s, lim):
    global N
    if len(s) == N:
        print("".join(s))
        # s = []
        return
    for i in range(lim): # abacなどが入らない
        s.append(A[i])
        dfs(s, len(set(s)) + 1)
        s.pop()
    # print(s)
    # dfs(s, prev)
    # s[-1] = A[prev + 1]
    # dfs(s, n+1, prev+1)

def main():
    global N
    N = int(input().strip())
    dfs([], 1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = ['a']
S = 'abcdefghij'
for _ in range(n-1):
    A = [ a + s for a in A for s in S[:len(set(a)) + 1]]
print('\n'.join(A))

Submission status: Accepted

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

N = int(input())

chars = "abcdefghij"
queue = deque()
queue.append(("a", 1))

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:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    if n == 1:
        return "a"
    r = []
    def f(t, k):
        u = chr(ord(k)+1)
        lf = len(t) == n - 1
        for c in string.ascii_lowercase:
            if c > u:
                break
            if lf:
                r.append(JA(t+[c], ""))
            else:
                if c > k:
                    f(t+[c], c)
                else:
                    f(t+[c], k)
    f(['a'], 'a')

    return JA(r, "\n")


print(main())

Submission status: Accepted

Solution code:
def string_equivalence(n):
    ans = [[]]
    s = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
    for _ in range(n):
        tmp = []
        for a in ans:
            for i in range(len(set(a))+1):
                a_tmp = []
                a_tmp.extend(a)
                a_tmp.append(s[i])
                tmp.append(a_tmp)
        ans = tmp
    for ans_s in ans:
        print(''.join(ans_s))

n = int(input())
string_equivalence(n)

Submission status: Accepted

Solution code:
def dfs(a='', b=chr(ord('a'))):
	if len(a) == N:
		print(a)
	else:
		i = chr(ord('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())
A = 'a'
S = 'abcdefghij'
for _ in range(n-1):
    A = [ a + s for a in A for s in S[:len(set(a)) + 1]]
print('\n'.join(A))

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline()[:-1]


if __name__ == "__main__":
    N = int(input())
    al = [chr(ord('a') + i) for i in range(26)]

    def dfs(s, mx):
        if (len(s) == N):
            print(s)
        else:
            for c in al:
                if c == chr(ord(mx) + 1):
                    break
                nmx = chr(ord(mx) + 1) if (c == mx) else mx  # 次のmx
                dfs(s + c, nmx)

    dfs('', 'a')

Submission status: Accepted

Solution code:
from collections import defaultdict, deque

def dfs(q, M, d):
    if d==N:
        res = []

        for qi in q:
            res.append(dic[qi])
        
        print(''.join(res))
        return
        
    for i in range(M+1):
        q.append(i)
        dfs(q, M, d+1)
        q.pop()
    
    q.append(M+1)
    dfs(q, M+1, d+1)
    q.pop()

N = int(input())
dic = defaultdict(int)
c = 0

for a in 'abcdefghijklmnopqrstuvwxyz':
    dic[c] = a
    c += 1

dfs(deque([0]), 0, 1)

Submission status: Accepted

Solution code:
n = int(input())
A = 'a'
S ='abcdefghij'
for _ in range(n-1):
    A = [a + s for a in A for s in S[:len(set(a))+1]]
print(*A, sep='\n')

Submission status: Accepted

Solution code:
n = int(input())
A = ['a']
S = 'abcdefghij'
for _ 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

sys.setrecursionlimit(100000)


def dfs(s, mx):
    if len(s) == N:
        print(s)
        return 0
    for c in range(ord("a"), mx + 2):
        t = s
        t += chr(c)
        dfs(t, max(mx, c))


N = int(input())
dfs("", ord("a") - 1)

Submission status: Accepted

Solution code:
N=int(input())
import itertools
L=[N-i for i in range(N,0,-1) for j in range(i)]
a=[0]*N
stop=[i for i in range(N)]
dict=[chr(ord('a')+i) for i in range(N)]
def content(a):
  ans=''
  for i in a:
    ans+=dict[i]
  return ans
print(content(a))
def saiki(a,p):
  nowp=N-1
  nowv=max(a)+1
  x=a[:]
  #print('a',p,nowp)
  while nowp>p:
    #print('b',p,nowp)
    if nowp==N-1:
      for X in range(nowv+1):
        a[nowp]=X
        print(content(a))
        #a=saiki(a,nowp)
    else:
      for X in range(1,nowv+1):
        a[nowp]=X
        a=saiki(a,nowp)
    a[nowp]=0
    nowp-=1
  return x
P=N-1
if N>1:
  a[-1]+=1
  print(content(a))
while P>0:
  a=saiki(a,P)
  P-=1
  a=[0]*N
  a[P]=1

Submission status: Accepted

Solution code:
#=============================================================================
# パナソニックコンテスト D
#=============================================================================
import sys
sys.setrecursionlimit(10**8)
N=int(input())
abc=list("abcdefghij")
def dfs(s,v):
    global N
    for i in range(v+2):
        ns=s+abc[i]
        if len(ns)<N :
            if i!=v+1:
                dfs(ns,v)
            else:
                dfs(ns,v+1)
        if len(ns)==N:
            print("".join(ns))
if N==1:
    print("a")
else:
    dfs("a",0)

Submission status: Accepted

Solution code:
n = int(input())
A= 'a'
# 文字数を増やして, その各々の元に対して文字を追加する.
for _ in range(n - 1):
    A = {a + s for a in A for s in a + chr(ord(max(a)) + 1)}
    # print(A)
# 最後にsortして調整する.
#print(*A, sep = '\n')
print(*sorted(A), sep='\n')

Submission status: Accepted

Solution code:
#pypyは内包表記使わない, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    N = int(input())
    d = deque(['a'])
    while len(d[0])<N:
        s = d.popleft()
        diff = ord(max(s)) - ord('a')
        for i in range(diff+2):
            x = ord('a')
            d.append(s+chr(x+i))
    while len(d)!=0:
        print(d.popleft())

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
A= 'a'
n = int(input())
# 二つのリストを独立に動かす.
# max('ab') は文字列のアルファベット順での最大値
for _ in range(n - 1):
    # max(a)の次のアルファベットを追加してsorted　で辞書順に
    A = [a + s for a in A for s in sorted(set(a + chr(ord(max(a))+1))) ]
print(*A, sep = '\n')

Submission status: Accepted

Solution code:
n = int(input())

ans = ["a"]

for i in range(n-1):
    old = ans
    ans = []
    for t in old:
        k = len(set(list(t)))
        for j in range(k+1):
            ans.append(t+chr(97+j))

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

Submission status: Accepted

Solution code:
def saiki(s, n, m):
  if n == 0:
    print(s)
    return
  
  a = ord('a')
  c = ord(m) + 2
  
  
  for i in range(a, c):
    saiki(s+chr(i), n-1, chr(max(i, ord(m))))
    
n = int(input())

saiki('a',n-1, 'a')

Submission status: Accepted

Solution code:
N=int(input())
from collections import deque
q=deque([["a",1]])
while q:
  ch,most=q.popleft()
  if len(ch)==N:
    print(ch)
    continue
  for i in range(ord("a"),ord("a")+most):
    q.append([ch+chr(i),most])
  q.append([ch+chr(ord("a")+most),most+1])

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    from operator import itemgetter
    from string import ascii_lowercase

    N = int(input())

    deq = deque(), deque()
    curr = 0
    deq[curr].append(('a', 0))  # (word,largest_char_index)
    for _ in range(N - 1):
        while deq[curr]:
            word, largest_char_index = deq[curr].popleft()
            for add_char_index in range(largest_char_index + 1 + 1):
                new_word = word + ascii_lowercase[add_char_index]
                deq[curr ^ 1].append(
                    (new_word, max(largest_char_index, add_char_index)))
        curr ^= 1

    print(*map(itemgetter(0), deq[curr]), sep='\n')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

eng = "abcdefghijklmnopqrstuvwxyz"

def range_dic(inp_dic):
    ans_dic = {}
    for key, value in inp_dic.items():
        for i in range(30):
            if i <= key:
                w = eng[i]
                _value = [v + w for v in  value]
                if not key in ans_dic:
                    ans_dic[key] = []
                ans_dic[key].extend(_value)
            elif i == key + 1:
                w = eng[i]
                _value = [v + w for v in  value]
                if not i in ans_dic:
                    ans_dic[i] = []
                ans_dic[i].extend(_value)
    return ans_dic


inp_dic = {0: ["a"]}

for i in range(N-1):
    inp_dic = range_dic(inp_dic)
results = []
for words in inp_dic.values():
    results.extend(list(words))

results.sort()
for result in results:
    print(result)

Submission status: Accepted

Solution code:

N = int(input())
def dfs(s, l, char_num):
    if l == N:
        print(s)
    else:
        for i in range(char_num+1):
            ns = s+chr(97+i)
            dfs(ns, l+1, len(set(ns)))

dfs('a',1, 1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(300000)


def solve(N: int):
    def _convert(cur):
        ret = [''] * N
        for i, pos in enumerate(cur):
            c = chr(ord('a') + i)
            for p in pos:
                ret[p] = c
        ret = ''.join(ret)
        return ret
    def rec(idx, cur):
        if idx == N:
            ret = _convert(cur)
            return [ret]
        ret = []
        for c in cur:
            c.append(idx)
            tmp = rec(idx + 1, cur)
            ret += tmp
            c.pop()
        cur.append([idx])
        ret += rec(idx + 1, cur)
        cur.pop()

        return ret
    ret = rec(0, [])
    for r in ret:
        print(r)
    #print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

def dfs(s, mx):
    """
    :param s: 今の文字
    :param mx: 今までで最大の文字
    :return:
    """
    if len(s) == n:
        print(s)
        return
    for c in range(ord('a'), ord(mx) + 1 + 1):
        t = s
        t += chr(c)
        mx = chr(max(ord(mx), c))
        dfs(t, mx)

dfs('', chr(ord('a')-1))

Submission status: Accepted

Solution code:
N = int(input())

def dfs(n,i,s,ans):
    if n == N:
        ans.append(s)
        return
    
    for j in range(i+1):
        tmp = s + chr(j + 97)
        max_j = ord(max(tmp)) - 96
        dfs(n+1,max_j,tmp,ans) 
    

def main():
    ans = []
    dfs(1,1,'a',ans)
    ans.sort()
    for a in ans:
        print(a)


    


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
def dfs(x):
    if len(x) == n:
        print(''.join(map(lambda i: chr(ord('a') + i - 1), x)))
        return
    for i in range(max(x)+1):
        y = x.copy()
        y.append(i+1)
        dfs(y)
dfs([1])

Submission status: Accepted

Solution code:
n = int(input())
ans = []
def dfs(x):
    if len(x) == n:
        print(''.join(map(lambda i: chr(ord('a') + i - 1), x)))
        return
    for i in range(max(x)+1):
        y = x.copy()
        y.append(i+1)
        dfs(y)
dfs([1])

Submission status: Accepted

Solution code:
n = int(input())

ans = []
def dfs(x):
    if len(x) == n:
        ans.append(x)
        return
    for i in range(max(x)+1):
        y = x.copy()
        y.append(i+1)
        dfs(y)
dfs([1])
#print(ans)

for ai in ans:
    print(''.join(map(lambda x: chr(ord('a') + x - 1), ai)))

#r = ['a','b','c','d','e','f','g','h','i','j']
#for ai in ans:
#    print(''.join(map(lambda x: str(r[x-1]), ai)))