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

al = [chr(ord('a') + i) for i in range(26)]

def dfs(S, i):
    if len(S) == N:
        yield ''.join(S)
        return
    for j in range(i):
        for w in dfs(S + [al[j]], i):
            yield w
    for w in dfs(S + [al[i]], i + 1):
        yield w

for w in dfs([], 0):
    print(w)

Submission status: Accepted

Solution code:
# coding: utf-8

N = int(input())
alphabets = 'abcdefghij'

def dfs(s, mx):
    if len(s) == N:
        print("".join(s))
    else:
        t = min(len(set(s))+1,len(s)+1)
        for c in range(t):
            dfs(s + [alphabets[c]], c)

if __name__ == "__main__":
    dfs([], 0)

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)+2):
            tmp = s + chr(i)
            dfs(tmp, max(mx, chr(i)))
dfs("a", "a")

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:
from collections import deque
d = 'abcdefghijklmnopqrstuvwxyz'
n = int(input())
ans = []
def dfs(x):

    que = deque(['a'])
    cnt = 1
    while len(que) != 0:
        val = que.pop()
        #print(val)
        if len(val) != x:
            t = list(val)
            t.sort()
            cnt = ord(t[-1]) - ord('a')
            for i in range(cnt+2):
                que.append(val+d[i])
        else:
            ans.append(val)
    ans.sort()
    return ans

ans = dfs(n)
for i in range(len(ans)):
    print(ans[i])

Submission status: Accepted

Solution code:
def dfs(base, dic, swap, ma):
    if len(base) == ma:
        print(base)
    else:
        num = max(map(lambda x: swap[x], base)) + 1
        for i in range(num + 1):
            dfs(base + dic[i], dic, swap, ma)


n = int(input().strip())
dic = {0: "a",
       1: "b",
       2: "c",
       3: "d",
       4: "e",
       5: "f",
       6: "g",
       7: "h",
       8: "i",
       9: "j",
       }
swap = {v: k for k, v in dic.items()}
dfs("a", dic, swap, n)

Submission status: Accepted

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


def dfs(s, i):
    # s:文字列 i:最大のアルファベットのindex
    if len(s) == N:
        print(s)
    else:
        for k in range(i+2):
            if k == i+1:
                dfs(s+w[k], i+1)
            else:
                dfs(s+w[k], i)


x = dfs('a', 0)

Submission status: Accepted

Solution code:
n = int(input())
def dfs(s,c):
    if len(s)==n:
        print(s)
        return
    for i in range(ord("a"),ord(c)+2):
        if i>ord(c):
            dfs(s+chr(i),chr(i))
        else:
            dfs(s+chr(i),c)

dfs("a","a")

Submission status: Accepted

Solution code:
N = int(input())
if N==1:
    print("a")
    exit()
def dfs(s,cnt):
    if len(s)==N:
        return s
    for i in range(cnt+1):
        if i==cnt:
            cnt+=1
        if dfs(s+chr(ord("a")+i),cnt)!=None:
            ans.append(dfs(s+chr(ord("a")+i),cnt))
ans=[]
dfs("a",1)
ans = sorted(ans)
print(*ans)

Submission status: Accepted

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

def dfs(res,i):
    if len(res)==n:
        print(res)
        return
    for k in range(i):
        dfs(res+l[k],i)
    dfs(res+l[i],i+1)

dfs('',0)

Submission status: Accepted

Solution code:
n=int(input())
l=[[] for i in range(n)]
l[0]=[[0]]
for i in range(n-1):
  for j in l[i]:
    for k in range(max(j)+2):
      l[i+1].append(j+[k])

for i in range(len(l[n-1])):
  for j in range(n):
    print(chr(l[n-1][i][j]+97), end="")
  print()

Submission status: Accepted

Solution code:
N = int(input())
chl = "abcdefghijk"
chli = {
  'a':0,
  'b':1,
  'c':2,
  'd':3,
  'e':4,
  'f':5,
  'g':6,
  'h':7,
  'i':8,
  'j':9
}

def ret(M):
  if M == 1:
    return "a"
  retl = []
  for stra in ret(M-1):
    maxi = min(chli[sorted(stra)[-1]] + 2, 10)
    cand = chl[:maxi]
    for ch in cand:
      retl.append(stra + ch)
  return retl

ans = ret(N)

for x in ans:
  print(x)

Submission status: Accepted

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

def dfs(res,i):
    if len(res)==n:
        print(res)
        return
    for k in range(i):
        dfs(res+l[k],i)
    dfs(res+l[i],i+1)

dfs('',0)

Submission status: Accepted

Solution code:
N=int(input())
alphabets = [chr(ord('a') + x) for x in range(26)]
def dfs(n,s,maxc):
    if len(s)==N:
        print(''.join(s))
        return
    for i in range(maxc+1):
        dfs(n+1,s+[alphabets[i]],maxc+1 if i==maxc else maxc)
s=[]
dfs(0,s,0)

Submission status: Accepted

Solution code:
N=int(input())
alphabets = [chr(ord('a') + x) for x in range(26)]
def dfs(n,s,maxc):
    if len(s)==N:
        print(''.join(s))
        return True
    for i in range(maxc+1):
        if i==maxc:
            maxc+=1
        dfs(n+1,s+[alphabets[i]],maxc)
s=[]
dfs(0,s,0)

Submission status: Accepted

Solution code:
from string import ascii_lowercase as alph

def gen_pattern(N, pre="", m=0):
  if len(pre)+1 == N:
    for c in alph[:m+1]:
      yield pre+c
  else:
    for c in alph[:m]:
      yield from gen_pattern(N, pre+c, m)
    yield from gen_pattern(N, pre+alph[m], m+1)

for pattern in gen_pattern(int(input())):
  print(pattern)

Submission status: Accepted

Solution code:
def gen_pattern(N, pre=""):
  for c in range(0x61, ord(max(pre or "\x60"))+2):
    pat = pre+chr(c)
    if len(pat) == N:
      yield pat
    else:
      yield from gen_pattern(N, pat)

for pattern in gen_pattern(int(input())):
  print(pattern)

Submission status: Accepted

Solution code:
from string import ascii_lowercase as alph

def gen_pattern(N, pre=""):
  for c in alph[:ord(max(pre or "\x60"))-0x5f]:
    pattern = pre+c
    if len(pattern) == N:
      yield pattern
    else:
      yield from gen_pattern(N, pattern)

for pattern in gen_pattern(int(input())):
  print(pattern)

Submission status: Accepted

Solution code:
from string import ascii_lowercase as alph

def gen_pattern(N, pre="", m=-1):
  for i in range(m+2):
    pat = pre+alph[i]
    if len(pat) == N:
      yield pat
    else:
      yield from gen_pattern(N, pat, max(i,m))

for pattern in gen_pattern(int(input())):
  print(pattern)

Submission status: Accepted

Solution code:
from string import ascii_lowercase as alph

def gen_pattern(N, pre="", m=-1):
  for i, c in enumerate(alph[:m+2]):
    pat = pre+c
    if len(pat) == N:
      yield pat
    else:
      yield from gen_pattern(N, pat, max(i,m))

for pattern in gen_pattern(int(input())):
  print(pattern)

Submission status: Accepted

Solution code:
from string import ascii_lowercase as alph

def gen_pattern(N, pre=""):
  for i in range(ord(max(pre or "\x60"))-0x5f):
    pattern = pre+alph[i]
    if len(pattern) == N:
      print(pattern)
    else:
      gen_pattern(N, pattern)

gen_pattern(int(input()))

Submission status: Accepted

Solution code:
from string import ascii_lowercase as alph
from operator import itemgetter

def gen_pattern(N, pre=[]):
  for i in range(max(pre or [-1])+2):
    indexes = pre+[i]
    if len(indexes) == N:
      print("".join(itemgetter(*indexes)(alph)))
    else:
      gen_pattern(N, indexes)

gen_pattern(int(input()))

Submission status: Accepted

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

num = int(input())
alf = ['a','b','c','d','e','f','g','h','i','j','k']
def dfs(s, mx_idx):
    if len(s) == num:
        print(s)
        return
    for i in range(mx_idx+1):
        if i == mx_idx:
            dfs(s+alf[i], mx_idx+1)
        else:
            dfs(s+alf[i], mx_idx)

dfs('', 0)

Submission status: Accepted

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

def dfs(s, count):
    if len(s) == N:
        print(s)
        return
    for i in range(count + 1):
        dfs(s+abc[i], count+1 if i == count else count)

dfs('', 0)

Submission status: Accepted

Solution code:
n = int(input())

if n == 1:
    print('a')
    exit()

def dfs(s, cmax):
    if len(s) == n:
        print(s)
    else:
        for i in range(ord(cmax)-ord('a')+2):
            c = chr(i+ord('a'))
            if ord(c) < ord(cmax):
                dfs(s+c, cmax)
            else:
                dfs(s+c, c)

dfs('a', 'a')

Submission status: Accepted

Solution code:
N = int(input())

al = 'abcdefghijk'


def next(digits, kind, cur):
    if digits == N:
        print(cur)
        return
    for i in range(kind+1):
        if i == kind:
            next(digits+1, kind+1, cur+al[i])
        else:
            next(digits+1, kind, cur+al[i])


next(1, 1, 'a')

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
N = int(input())
def f(n,s):
    if n == 0:
        print(s)
        return
    for i in [chr(ord("a") + i) for i in range(N-n+1)]:
        mx = 0
        for j in s:
            mx = max(mx,ord(j))
        if s != "" and ord(i) - mx > 1:
            return 
        f(n-1,s+i)
f(N,"")

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)#再帰を使うときはこれをする（今回はN=10で深さ110000程度なので10**6)
def dfs(s,mx):
    if len(s) == n:
        print(s)
        return
    for i in range(ord("a"),ord(mx)+1):
        c = chr(i)
        if c == mx:dfs(s+c,chr(ord(mx)+1))#aから一番遠いアルファベットを語尾につけたら、その次までok
        else:dfs(s+c,mx)
n = int(input())
dfs("","a")

Submission status: Accepted

Solution code:
n = int(input())
strings = [[] for _ in range(n)]
for i in range(n):
    if not i:
        strings[i].append("a")
        continue
    for s in strings[i-1]:
        for j in range(ord("a"),ord(max(s))+2):
            c = chr(j)
            strings[i].append(s+c)
print(*strings[-1])

Submission status: Accepted

Solution code:
L=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i','j']
L_ind = [i for i in range(10)]
N=int(input())
K=[]
for i in range(N):
    if i==0:
        K.append(str(L_ind[0]))
    else:
        K_ = []
        for j in K:
            for m in range(int(max(set(j)))+2):
                K_.append(j + str(L_ind[m]))
        K = K_
    
for k in K:
    ans = ''
    for i in k:
        ans += L[int(i)]
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())

alphabet = 'abcdefghij'
s = [-1 for i in range(n)]
lst = [[] for i in range(n)]
lst[0].append(0)
ind = 0
while lst[ind] != [] or ind > 0:
    if ind < n-1 and lst[ind] != []:
        s[ind] = lst[ind].pop()
        for i in range(max(s)+1, -1, -1):
            lst[ind+1].append(i)
        ind += 1
    else:
        while lst[ind] != []:
            s[ind] = lst[ind].pop()
            ans = [alphabet[i] for i in s]
            print(''.join(ans))
        s[ind] = -1
        ind -= 1

Submission status: Accepted

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

d = deque(['a'])

while d:
    w = d.popleft()
    if len(w) == n:
        print(w)
    else:
        w_m = len(set(w))
        for i in range(w_m + 1):
            d.append(w + chr(ord('a') + i))

Submission status: Accepted

Solution code:
def dfs(s,x):
    if len(s) == n:
        print(s)
    else:
        for c in range(ord('a'), ord(x) + 1):
            dfs(s + chr(c), chr(ord(x) + 1) if chr(c) == x else x)

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

Submission status: Accepted

Solution code:
n=int(input())

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

dfs("a",ord("a"))

Submission status: Accepted

Solution code:
n = int(input())

alphabet = 'abcdefghij'
s = [-1 for i in range(n)]

def dfs(ind, r):
    if ind == n:
        ans = [alphabet[i] for i in s]
        print(''.join(ans))
        return
    for i in range(r):
        s[ind] = i
        dfs(ind+1, max(s)+2)
        s[ind] = -1

dfs(0, 1)

Submission status: Accepted

Solution code:
s="abcdefghijklmnopqrstuvwxyz"
import sys
sys.setrecursionlimit(10**6)
def dfs(a,n,m):
    if len(a)==n:
        print(a)
        return
    for i in range(m):
        if i==m-1:
            dfs(a+s[i],n,m+1)
        else:
            dfs(a+s[i],n,m)
n=int(input())
dfs("",n,1)

Submission status: Accepted

Solution code:
alp = [chr(i) for i in range(97, 97+26)]

def dfs(s, max_):
    if len(s)==N:
        print(s)
    else:
        for i in range(max_+1):
            if i==max_:
                dfs(s+alp[i], max_+1)
            else:
                dfs(s+alp[i], max_)
                
        
N = int(input())
dfs('',0)

Submission status: Accepted

Solution code:
N=int(input())
S="a"
bottom=""
alpha="abcdefghijklmnopqrstuvwxyz"

for i in range(N):
    if i>25:
        bottom=bottom+"z"
    else:
        bottom=bottom+alpha[i]
    S=S+"a"

C=0
S=""
def F(S,loop,N,C):
    for i in range(0,loop):
        if N==C+1:
            print(S+bottom[i])
        else:
            if loop==i+1:
                loop+=1
            F(S+bottom[i],loop,N,C+1)
    return None
            
F(S,1,N,C)

Submission status: Accepted

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

for x in range(1,n):
  for y in res[x-1]:
    for c in range(10):
      if cha[c] not in y:
        for d in range(c+1):
          res[x].append(y+cha[d])
        break
  if x > n + 1:
    break

for ans in range(len(res[n-1])):
  print(res[n-1][ans])

Submission status: Accepted

Solution code:
## coding: UTF-8
N = int(input())
#N = 4
s = "abcdedcba" #よく更新する文字列
answer = ['a'] #回答となる文字列のリストを入れる場所 　初期値はN=1の['a']を入れておきます。

#文字列の中から辞書順で最大のアルファベットを検索するコード
def search_max(s):
    max = 'Z' #大文字の方が文字コードが若い。aよりも若いものを最大値として押さえておく
    for i in range(len(s)):
        if(s[i] > max):
            max = s[i]
    return max

#アルファベットに対して次の文字を返すコードではなく
#アルファベットに対して次の文字までのリストを返す
def next(p):
    list_alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
    alpha_index = -100
    for i in range(len(list_alpha)):
        if(p == list_alpha[i]):
            alpha_index = i
    #return list_alpha[alpha_index + 1]
    return list_alpha[:alpha_index+2]



#n文字の文字列（回答）が入ったリストを受け取ったら、その各文字列に対して1文字付け足して返す関数
def one_plus(l): 
    ret_list = [] #最終的に返すリスト
    for i in range(len(l)): #listの各要素に対して
        max_alpha = search_max(l[i]) #まずは要素の辞書順最大アルファベットを探す
        next_list = next(max_alpha) #次に入るアルファベット候補の羅列
        #print(next_list)
        for j in range(len(next_list)):
            ret_list.append(l[i] + next_list[j])
    return ret_list

for i in range(N-1):
    answer = one_plus(answer) #answerリストに1文字ずつ付け足して返す
#print(answer)

for i in range(len(answer)):
    print(answer[i]) #1行ずつ出力

'''
def sum(n):
    if n < 1:
        return n
    return n + sum(n-1)

si = sum(100)
print("1から100の合計は", si, "です")
'''
#print(search_max(s))
#print(next('a'))
#print(next('c'))
#print(one_plus(["aa", 'ab']))

Submission status: Accepted

Solution code:
import sys,math
sys.setrecursionlimit(10**9)
import itertools
from collections import defaultdict
 
IA = lambda: map(int,input().split())
IAS= lambda: map(str,input().split())


n=int(input())


def dfs(s,lim):

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

dfs('','a')

Submission status: Accepted

Solution code:
n = int(input())
q = [("0",0)]
cnt = 0
while cnt < n - 1:
    qt = []
    for numset in q:
        num = numset[0]
        maxnum = numset[1]
        for i in range(int(maxnum) + 2):
            mx = max(maxnum,i)
            qt += [(num + str(i), mx)]
    q = qt.copy()
    cnt += 1
    
alp = "abcdefghijkl"
    
def show(num):
    ret = ""
    l = len(num)
    for i in range(l):
        ret += alp[int(num[i])]
    return ret

q.sort()

for num in q:
    print(show(num[0]))

Submission status: Accepted

Solution code:
from collections import deque
import sys
sys.setrecursion = 10000000

n = int(input())
alpha = "abcdefghijklmnopqrstuvwxyz"
def dfs(str,letter):
  if len(str) == n:
    print(str)
  else:
    length = len(set(str))
    for let in range(length+1):
      dfs(str+alpha[let],let)

def solve(): 
  query = deque([])
  dfs("a",1)


solve()

Submission status: Accepted

Solution code:
N = int(input())
alphabet_list = [chr(i) for i in range(97, 97 + N)]

import collections

previous_list = ["a"]
for i in range(1, N):
    out_list = []
    for a in previous_list:
        # print(len(collections.Counter(a)))
        for al in alphabet_list[:len(collections.Counter(a))+1]:
            # print(a + al)
            out_list.append(a+al)
    previous_list = out_list

for out in previous_list:
    print(out)

Submission status: Accepted

Solution code:
n = int(input())
a1 = list("abcdefghij")

b1 = [[0]]
c1 = 1
ans = []
while c1 < n:
    c1 += 1
    b2 = b1 + []
    b1 = []
    for i in b2:
        imax = max(i)
        for j in range(imax+2):
            b1.append(i+[j])

for i in b1:
    ans = [a1[j] for j in i]
    print("".join(ans))

Submission status: Accepted

Solution code:
n = int(input())

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

dfs('', 0)

Submission status: Accepted

Solution code:


def dfs(str, max, rast):
    if(rast==0):
        return 0
    else:
        for i in range(ord('a'), max+1):
            if(rast==1):
                print(str + chr(i))
            if(i==max):
                dfs(str+chr(i), max+1, rast-1)
            else:
                dfs(str+chr(i), max, rast-1)


def main():
    n = int(input())
    s = 'a'
    if(n==1):
        print(s)
    dfs(s, ord(s[-1])+1, n-1)

main()

Submission status: Accepted

Solution code:
N = int(input())
 
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
 
def solve(n):
    if n == 1:
        return ['a']
    elif n == 2:
        return ['aa', 'ab']
    else:
        chars = alphabets[0:n]
        result = []
        for retsu in solve(n - 1):
            done = False
            for char in chars:
                if char in retsu:
                    result.append(retsu + char)
                else:
                    if not done:
                        result.append(retsu + char)
                        done = True
                    else:
                        break
        return result
 
print("\n".join(solve(N)))

Submission status: Accepted

Solution code:
n = int(input())

alpha = [chr(i) for i in range(97, 97+26)]

def dfs(s, mx):
    if(len(s)==n):
        print(s)
    else:
        for i in range(mx):
            if i==mx-1:
                dfs(s+alpha[i], mx+1)
            else:
                dfs(s+alpha[i], mx)

dfs("",1)

Submission status: Accepted

Solution code:
N = int(input())
F="abcdefghijklmnopqrstuvwxyz"
def cy(ans,l):
  
  if len(ans)==N:
    print(ans)
  else:
    for i in range( min(l+2,27) ):
      k=ans+F[i]
      cy(k,max(l,i))

cy("a",0)

Submission status: Accepted

Solution code:
N = int(input())

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

def search(arr):
  global N, char
  if len(arr) == N:
    ans = ''.join([char[x] for x in arr])
    print(ans)
    return
  else:
    for i in range(max(arr) + 2):
      arr_next = arr + [i]
      search(arr_next)

search([0])

Submission status: Accepted

Solution code:
c = 'abcdefghij'

def join(s, k, n):
    if len(s) == n:
        return [s]
    ans = []
    for i in range(k):
        ans += join(s + c[i], k, n)
    ans += join(s + c[k], k + 1, n)
    return ans

n = int(input())
ans = join('', 0, n)
for x in ans:
    print(x)

Submission status: Accepted

Solution code:
n = int(input())

ans = [0] * n

def prc(L):
    res = []
    for x in L:
        res.append(chr(ord('a') + x))
    print(''.join(res))

while 1:
    prc(ans)
    for i in range(n-1, 0, -1):
        temp = max(ans[:i])
        if ans[i] <= temp:
            ans[i] += 1
            for j in range(i+1, n):
                ans[j] = 0
            break
    else:
        break

Submission status: Accepted

Solution code:
n = int(input())

def chrinc(s,n):
  return chr(ord(s)+1)

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

dfs('','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
from fractions import gcd
 
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()
l = "abcdefghijk"

def dfs(cur, mx):
    if len(cur) == N:
        print(cur)
        return
    # print(l[:mx+2])
    for c in range(len(l[:mx+2])):
        t = cur
        t += l[:mx+2][c]
        # print(cur)
        dfs(t, max(c, mx))
        # dfs(cur, max(c, mx+1))

dfs("", -1)

Submission status: Accepted

Solution code:
import collections

N = int(input())

W = ["a"]
nW = []

"""
k = len(collections.Counter(Y[0]))
print(k)
"""
for j in range(N-1):
    for w in W:
        k = len(collections.Counter(w))
        for i in range(k+1):
            nW.append(w + chr(i+97))
    W = nW.copy()
    nW.clear()
    
    # print("N=",j+2)
# print(W[-1])

for i in W:
    print(i)

Submission status: Accepted

Solution code:
import itertools as iters

def enlong(s,m):
    return [s + [i] for i in range(1,m+2)]

N = int(input())
alpha = "abcdefghijk"
R = [[1]]
for _ in range(N-1):
    S = []
    for r in R:
        S += enlong(r,max(r))
    R = S[::]
for r in R:
    s = "".join([alpha[ss-1] for ss in r])
    print(s)

Submission status: Accepted

Solution code:
n = int(input())

lets = [chr(i) for i in range(ord("a"), ord("z") + 1)]
ans = []


def dfs(s, l, mx):
    if l == n:
        ans.append(s)
        return

    for i in range(mx + 2):
        dfs(s + lets[i], l + 1, max(mx, i))


dfs("", 0, -1)
print(*ans, sep="\n")

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 range(length):
            ttmp = sorted(list(S[i]))
            for j in range(alphabet.index(ttmp[-1])+2):
                tmp.append(S[i]+alphabet[j])
        S = tmp
        f(num+1,N,S)
 
N = int(input())
f(1,N,["a"])

Submission status: Accepted

Solution code:
#!python3

import sys
sys.setrecursionlimit(200000)


# input
N = int(input())
l = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]


def solve(s, m):
    if len(s) == N:
        print(s)
    else:
        for i in range(m + 1):
            solve(s + l[i], max(i + 1, m))


def main():
    solve("a", 1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
def mojiOut(s, mx):
    if len(s) == N:
        print(s)
        return

    for i in range(ord("a"), ord(mx) + 2):
        strS = s
        strS += chr(i)
        mojiOut(strS, max(mx, chr(i)))

mojiOut("a",  "a")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()

ans = set()
def rec(i, S):
    if S in ans:
        return
    if i == N:
        ans.add(S)
        return
    # 今までに出現した文字は全部追加できる
    for c in S:
        rec(i+1, S+c)
    # 今までに出現した中で最大の文字の次も追加できる
    rec(i+1, S+chr(ord(max(S))+1))
rec(1, 'a')
ans = sorted(ans)
[print(s) for s in ans]

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) + 2):
            tmp = s
            tmp += chr(i)
            dfs(tmp, max(mx, chr(i)))

dfs("a", "a")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()

ans = set()
def rec(i, S):
    if S in ans:
        return
    if i == N:
        ans.add(S)
        return
    # 今までに出現した文字は全部追加できる
    for c in S:
        rec(i+1, S+c)
    # 今までに出現した中で最大の文字の次も追加できる
    li = sorted(S)
    rec(i+1, S+chr(ord(li[-1])+1))
rec(1, 'a')
ans = sorted(ans)
[print(s) for s in ans]

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def bfs(s):
    if len(s) == n:
        print(s)
        return 
    ord_num = 97#aから順番に回す
    while True:
        #aから順番に埋めていく。sにないものがきたらそこまで再帰して終了
        char = chr(ord_num)
        if char in s:
            bfs(s+char)
        else:
            bfs(s+char)
            break#ここで終了
        ord_num += 1
if __name__ == "__main__":
    n = int(input())
    s = ""
    bfs(s)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
alpha = "abcdefghij"
ans = []
N = int(input())

def dfs(S, num):
    if len(S) == N:
        ans.append(S)
    else:
        right = num + 2
        seq = alpha[:right]
        for i, c in enumerate(seq):
            dfs(S+c, max(i, num))

dfs("a", 0)
ans = sorted(ans)
for a in ans:
    print(a)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
alpha = "abcdefghij"
ans = []
N = int(input())

def get_right(S):
    alpha_idx = 0
    for i in range(len(S)):
        flag = S.find(alpha[i])
        if flag > 0:
            alpha_idx = i
    return alpha_idx + 2


def dfs(S, num):
    if len(S) == N:
        ans.append(S)
    else:
        right = num + 2
        seq = alpha[:right]
        for i, c in enumerate(seq):
            dfs(S+c, max(i, num))

dfs("a", 0)
ans = sorted(ans)
for a in ans:
    print(a)

Submission status: Accepted

Solution code:
N = int(input())
ans = ["a"]
for i in range(N-1):
    new = []
    for j in range(len(ans)):
        l = ord(max(ans[j]))-95
        add = [chr(ord("a") + idx) for idx in range(l)]
        for k in add :
            new.append(ans[j] + k)
    ans = new

ans.sort()
for p in ans:
    print(p)

Submission status: Accepted

Solution code:
import sys
#input = sys.stdin.readline
input = sys.stdin.buffer.readline

# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np


def main():
	n=int(input())
	s="abcdefghijklmn"

	li=[[] for _ in range(10)]
	li[0]=[["a",0]]


	for i in range(1,10):

		for t,val in li[i-1]:
			for j in range(val+2):
				li[i].append([t+s[j],max(val,j)])

	for t,_ in li[n-1]:
		print(t)


if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
alpha = "abcdefghij"
ans = []
N = int(input())

def get_right(S):
    alpha_idx = 0
    for i in range(len(S)):
        flag = S.find(alpha[i])
        if flag > 0:
            alpha_idx = i
    return alpha_idx + 2


def dfs(S):
    if len(S) == N:

        ans.append(S)
    else:
        right = get_right(S)
        seq = alpha[:right]
        for c in seq:
            dfs(S+c)
    return

dfs("a")
ans = sorted(ans)
for a in ans:
    print(a)

Submission status: Accepted

Solution code:
import copy
n = int(input())

def dfs(str_list, now):
    if len(str_list) == n:
        print(''.join(str_list))
    else:
        chr_a = ord('a')
        for c in range(chr_a, now + 2, 1):
            str_list_cp = copy.deepcopy(str_list)
            str_list_cp.append(chr(c))
            dfs(str_list_cp, max(c, now))

dfs([], ord('a') - 1)

Submission status: Accepted

Solution code:
n = int(input())

def normal_form(x):
    if x == 1:
        return ['a']
    temp = normal_form(x-1)
    res = []
    for s in temp:
        for k in range(ord(max(s))-97+2):
            res.append(s+chr(k+97))
    return res

print('\n'.join(normal_form(n)))

Submission status: Accepted

Solution code:
n=int(input())
abc='abcdefghij'
def dfs(i,a,s):
    if i==n:
        print(s)
    else:
        for j in range(0,a+1):
            dfs(i+1,max(a,j+1),s+abc[j])
dfs(0,0,'')

Submission status: Accepted

Solution code:
import string

N = int(input())
alph = string.ascii_lowercase

def search(S, mx):
    if len(S) == N:
        yield S
    else:
        for s in alph[: mx]:
            for w in search(S + s, mx):
                yield w
        for w in search(S + alph[mx], mx + 1):
            yield w

ans = list(search('', 0))
print(*ans, sep='\n')