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())
ans = ['a'] # 最初の一文字は'a'で固定

for i in range(n-1): # 'a'に'n-1'文字分ループで足す
    tmp = [] # リセット
    for j in ans:
        l = len(set(j)) # 'j'の使用文字種の数をカウント
        for k in range(l+1): # 'j'の使用文字種の数 '+1'回ループして...
            tmp.append(j + chr(ord('a') + k)) # 'j'に appendする
    ans = tmp[:] # 'ans'を書き換える

for i in ans:
    print(i)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
dict = {1:"a", 2:"b", 3:"c", 4:"d", 5:"e", 6:"f", 7:"g", 8:"h", 9:"i", 10:"j"}
dict2 = {"a":1, "b":2, "c":3, "d":4, "e":5, "f":6, "g":7, "h":8, "i":9, "j":10}
#ans = [["a", 1]]
#for i in range(n-1):
#    newans = []
#    for lis in ans:
#        now = lis[0]
#        count = lis[1]
#        for j in range(1, count + 2):
#            newans.append([now + dict[j], max(j, count)])
#    ans = newans
#for lis in ans:
#    print(lis[0])
def dfs(s):
    if len(s) == n:
        print(s)
        return
    m = max(s)
    count = dict2[m]
    for i in range(1, count+2):
        dfs(s+dict[i])
dfs("a")

Submission status: Accepted

Solution code:
n = int(input())
dict = {1:"a", 2:"b", 3:"c", 4:"d", 5:"e", 6:"f", 7:"g", 8:"h", 9:"i", 10:"j"}
ans = [["a", 1]]
for i in range(n-1):
    newans = []
    for lis in ans:
        now = lis[0]
        count = lis[1]
        for j in range(1, count + 2):
            newans.append([now + dict[j], max(j, count)])
    ans = newans
for lis in ans:
    print(lis[0])

Submission status: Accepted

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

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

n = readint()

ans = [[[0],[0]]]

i = 0
while len(ans[i][0])<n:
    a = ans[i]
    for x in a[1]:
        ans.append([a[0]+[x],a[1]])
    x = a[1][-1]+1
    ans.append([a[0]+[x],a[1] + [x]])
    i+=1

a_ = ord('a')

for i in range(len(ans)):
    a = ans[i][0]
    if len(a)<n:
        continue
    for x in a[:-1]:
        print(chr(x+a_),end='')
    print(chr(a[-1]+a_))

Submission status: Accepted

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

for i in range(n-1):
    buf = []
    for j in ans:
        l = len(set(j))
        for c in range(l+1):
            buf.append(j+chr(ord('a')+c))
    ans = buf

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
def f(N):
    if N==1:
        return [[0]]

    X=f(N-1)
    Y=[]
    for x in X:
        k=max(x)
        for a in range(min(k+2,10)):
            Y.append(x+[a])
    return Y

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

for a in f(N):
    S=""
    for x in a:
        S+=T[x]
    print(S)

Submission status: Accepted

Solution code:
N = int(input())
 
alp = "abcdefghijklmnopqrstuvwxyz"
 
def dfs(s):
    i = len(s)
    if i == N:
        print(s)
    else:
        index = 0
        for k in range(len(s)):
            index = max(index, alp.index(s[k]))
        for j in range(index+2):
            dfs(s+alp[j])
 
dfs("a")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
l = "abcdefghij"


def dfs(a, mx):
    if len(a) == n:
        print(a)
        return
    for i, s in enumerate(l):
        if i == mx+1:
            dfs(a+s, mx+1)
            break
        else:
            dfs(a+s, mx)


n = int(input())
dfs("a", 0)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
q = deque(["a"])

# 最後に最新の文字数のセットだけ拾う
for i in range(n - 1):
    next_q = deque()
    for curr in q:
        suffix = ord(max(curr)) + 1
        for x in range(ord("a"), suffix + 1):
            next_q.append(curr + chr(x))
    q = next_q

print(*q, sep="\n")

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
if n == 1:
    print('a')
else:
    q = deque(['a'])
    while q:
        x = q.popleft()
        l = len(x)
        s = len(set(x))
        for i in range(s+1):
            nx = x + chr(i + 97)
            if l + 1 == n:
                print(nx)
            if l < n - 1:
                q.append(nx)

Submission status: Accepted

Solution code:
n = int(input())
ans = []
start = "a"
stack = []
stack.append(start)
num = ord("a")
while stack:
    v = stack.pop()
    if len(v) == n:
        ans.append(v)
        continue
    s = list(v[::])
    s.sort()
    nex_num = ord(s[-1])
    for i in range(num, nex_num + 2):
        tuika = chr(i)
        stack.append(v + tuika)
print(*ans[::-1], sep="\n")

Submission status: Accepted

Solution code:
n = int(input())
ans = []
start = ("a", 1, ord("a"))
stack = []
stack.append(start)
num = ord("a")
while stack:
    v, nagasa, mx = stack.pop()
    if nagasa == n:
        ans.append(v)
        continue
    for i in range(num, mx + 2):
        tuika = chr(i)
        mx = max(i, mx)
        stack.append((v + tuika, nagasa + 1, mx))
print(*ans[::-1], sep="\n")

Submission status: Accepted

Solution code:
n = int(input())
ans = []
start = "a"
stack = []
stack.append(start)
num = ord("a")
while stack:
    v = stack.pop()
    if len(v) == n:
        ans.append(v)
        continue
    s = list(v[::])
    s.sort()
    nex_num = ord(s[-1])
    for i in range(num, nex_num + 2):
        tuika = chr(i)
        stack.append(v + tuika)
ans.sort()
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
def dfs(i,sent):
    if len(sent)==N:
        print(sent)
        return
    if i>N:
        return
    for j in range(i+1):
        dfs(max(i,j+1),sent+chara[j])
    return

N=int(input())
chara=["a","b","c","d","e","f","g","h","i","j"]
chara=chara[:N]
ans=[]
dfs(1,"a")

Submission status: Accepted

Solution code:
nag = int(input())

def dfs(rootlist,deep,n):
    global nag
    if len(rootlist) != nag:
        for i in range(n+1):
            nextroot = rootlist[:]
            nextroot.append(i)
            if i < n:
                dfs(nextroot,deep+1,n)
            else:
                dfs(nextroot,deep+1,n+1)
    else:
        answer = ''
        wordlist = ['a','b','c','d','e','f','g','h','i','j']
        for index in rootlist:
            answer += wordlist[index]
        print(answer)


dfs([0],0,1)

Submission status: Accepted

Solution code:
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:
# -*- coding: utf-8 -*-
# D

import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
from itertools import combinations
input = sys.stdin.readline

# 再起回数上限変更
# sys.setrecursionlimit(1000000)

n = int(input())
if n == 1:
    print('a')
    sys.exit()


def dfs(s):
    if len(s) == n:
        print(''.join([chr(s + ord('a')) for s in s]))
    else:
        for i in range(len(set(list(s))) + 1):
            dfs(s + [i])
dfs([])

Submission status: Accepted

Solution code:
n = int(input())
def dfs(i,s):
    if i == n:
        print(s)
        return
    ma = 0
    for j in s:
        ma = max(ma,ord(j))
    for j in range(ma - 95):
        dfs(i+1,s+chr(97+j))
    return
dfs(1,"a")

Submission status: Accepted

Solution code:
n=int(input())

lis=[[[1,"a"]]]

for i in range(n-1):
    lisi=[]
    for j in lis[i]:
        for k in range(j[0]):
            lisi.append([j[0],j[1]+chr(ord("a")+k)])
        lisi.append([j[0]+1,j[1]+chr(ord("a")+j[0])])
    lis.append(lisi)

for i in lis[n-1]:
    print(i[1])

Submission status: Accepted

Solution code:
from collections import deque # BFSはこいつを使え！
def dfs(n):
    adj = 'abcdefghij'
    
    stack = deque(['a'])
    while stack:
        # node = stack.pop(0) # pop(0)がO(n)かかっているのでとても遅い！
        node = stack.popleft()
        if len(node) == n:
            print(node)
        else:
            
            for child in adj[:len(set(node))+1]:
                stack.append(node+child)
    return -1

# 再帰での実装
# def dfs(s,n,adj):
#     if len(s) == n:
#         print(s)
#     else:
#         for child in adj[:len(set(s))+1]:
#             dfs(s+child,n,adj)
#     return -1

def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    # adj = 'abcdefghij'
    # dfs('a',n,adj)
    dfs(n)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def dfs(s,n,adj):
    adj = 'abcdefghij'
    if len(s) == n:
        print(s)
    else:
        for child in adj[:len(set(s))+1]:
            dfs(s+child,n,adj)
    return -1

def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    adj = 'abcdefghij'
    dfs('a',n,adj)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
ans=[]
N=int(input())
def saiki(A):
    eigo="abcdefghijklmnopqrstuvwxyz"
    if len(A)==N:
        B="".join(A)
        ans.append(B)
        return
    for i in range(len(set(A))+1):
        A.append(eigo[i%26])
        saiki(A)
        A.pop()
saiki([])
ans.sort()
for i in ans:
    print(*i,sep="")

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement

N = int(input())
C = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
DP = [set() for _ in range(N)]
DP[0].add((1, 'a'))

for i in range(1, N):
    for num, s in DP[i-1]:
        for j in range(num):
            DP[i].add((num, s + C[j]))
        DP[i].add((num + 1, s + C[num]))
A = []
for _, ax in DP[N-1]:
    A.append(ax)
A.sort()
for a in A:
    print(a)

Submission status: Accepted

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


def dfs(s, i):
    if len(s) == n:
        return s
    return "\n".join([dfs(s + c, i)for c in alph[:i]] +\
                     [dfs(s + alph[i], i + 1)])


print(dfs("a", 1))

Submission status: Accepted

Solution code:
def dfs(s, ub, n):
    if len(s) == n:
        print(s)
    else:
        ch = "a"
        while ord(ch) < ord(ub):
            dfs(s + ch, ub, n)
            ch = chr(ord(ch) + 1)
        ub = chr(ord(ub) + 1)
        dfs(s + ch, ub, n)

def solve(n):
    dfs("", "a", n)

n = int(input())
solve(n)

Submission status: Accepted

Solution code:
n = int(input())

hyo = [[[1]]]

for i in range(n-1):
    n_hyo = []
    for arr in hyo[i]:
        for j in range(max(arr)+1):
            n_arr = arr[:]
            n_arr.append(j+1)
            n_hyo.append(n_arr)
    hyo.append(n_hyo)

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

for i in hyo[n-1]:
    ans = ''
    for j in i:
        ans += alp[j-1]
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
alpha=list('abcdefghijklmnopqrstuvwxyz')
res=['']
for i in range(N):
    tmp = []
    for j in res:
        for k in alpha[:len(set(list(j))) +1]:
            tmp.append(j+k)
    res = tmp

for i in res:
    print(i)

Submission status: Accepted

Solution code:
n = int(input())

l = []


def f(n):
    return chr(n+ord("a"))


def search(s: str, cnt: int) -> str:
    if len(s) == n:
        return s
    cnt = len(set(list(s)))
    for i in range(0, min(cnt, len(s)) + 1):
        g = search("".join([s, f(i)]), cnt + max(0, i - len(s) + 1))
        if g is not None:
            l.append(g)


search("", 1)

l.sort()

for s in l:
    print(s)

Submission status: Accepted

Solution code:
N = int(input())

#文字数分aで埋める
#末尾をbにする
#一桁上の文字+1文字までは許容するルールで増加させていく

ans = [0 for _ in range(N)]
stop = [i for i in range(N)]

def ans_print(ans_list):
    ans = list()
    for a in ans_list:
        ans.append(chr(a+97)) #97でaが出る

    print(''.join(map(str, ans)))
    return 0

def ans_check(ans):
    max_in_ans = 0

    for i in range(1,N):
        if max_in_ans + 1 < ans[i]:
            ans[i-1] += 1
            ans[i] = 0
            return ans_check(ans)
        else:
            max_in_ans = max(max_in_ans,ans[i])
    else:
        return ans

if N == 1:
    print("a")
else:
    while 1:
        ans_check(ans)
        ans_print(ans)
        ans[-1] += 1
        if ans == stop:
            ans_print(ans)
            break

Submission status: Accepted

Solution code:
N = int(input())
def sub(l, d):
  if d == N:
    s = "".join(["abcdefghijklmnopqrstuvwxyz"[i] for i in l])
    print(s)
    return
  for j in range(max(l)+1+1):
    sub(l+[j], d+1)
sub([0], 1)

Submission status: Accepted

Solution code:
n = int(input())

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

dfs('', 0)

Submission status: Accepted

Solution code:
from collections import deque
N=int(input())
d=deque()
d.append("a")
dall=deque()
dall.append(d)
i=0
while i<N-1:
    length=len(dall)
    j=0
    while j<length:
        l=dall.popleft()
        llen=len(set(l))
        for k in range(llen+1):
            q = deque()
            for m in range(len(l)):
                c = l.popleft()
                q.append(c)
                l.append(c)
            q.append(chr(ord("a")+k))
            dall.append(q)
        #print(dall)
        #input()
        j=j+1
    i=i+1
    #print(dall)
    #input()
#print(len(dall))
ans=[]
for i in range(len(dall)):
    print("".join(dall[i]))

Submission status: Accepted

Solution code:
from collections import deque
N=int(input())
d=deque()
d.append("a")
dall=deque()
dall.append(d)
i=0
while i<N-1:
    length=len(dall)
    j=0
    while j<length:
        l=dall.popleft()
        llen=len(set(l))
        for k in range(llen+1):
            q = deque()
            for m in range(len(l)):
                c = l.popleft()
                q.append(c)
                l.append(c)
            q.append(chr(ord("a")+k))
            dall.append(q)
        #print(dall)
        #input()
        j=j+1
    i=i+1
    #print(dall)
    #input()
#print(len(dall))
ans=[]
for i in range(len(dall)):
    ans.append("".join(dall[i]))
ans.sort()
for i in range(len(ans)):
    print(ans[i])

Submission status: Accepted

Solution code:
al = "abcdefghijklmnopqrstuvwxyz"
k  = int(input())
def dfs(s: "char", i):
    if(len(s)==k):
        print(s)
    else:
        for j in range(i+1):
            if al[j]==al[i]:
                dfs(s+al[j], i+1)
            else:
                dfs(s+al[j], i)




def main():
    #sの後ろを変えていくdfs
    dfs("", 0)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

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

dfs('a', 1)

Submission status: Accepted

Solution code:
from itertools import combinations

n = int(input())

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


dfs('a')

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/panasonic2020/tasks/panasonic2020_d

N = int(input())

alphabet = "abcdefghijklmnopqrstuvwxyz"

curr_str = []

def print_str(curr_str):
    string = ""
    for i in curr_str:
        string += alphabet[i]
    print(string)


def dfs(index, curr_str, curr_char):
    if curr_char > index:
        return None
    if curr_char > max(curr_str) + 1:
        return None

    if index == N-1:
        print_str(curr_str + [curr_char])
        return None

    for i in range(N):
        dfs(index+1, curr_str+[curr_char], i)


if N == 1:
    print("a")
else:    
    for i in range(N):
        dfs(1, [0], i)

Submission status: Accepted

Solution code:
n = int(input())
alp = [chr(ord("a") + x) for x in range(26)]

def dfs(S):
    if len(S) == n:
        print(S)
    else:
        max_s = ord(max(S)) - 96
        #print(max_s)
        for i in range(max_s + 1):
            dfs(S + alp[i])

dfs("a")

Submission status: Accepted

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

def f(x,c):
    if len(x) == n:
        print(x)
    else:
        for i in range(c):
            f(x+al[i],c)
        f(x+al[c],c+1)

n = int(input())

f("a",1)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
c = [chr(ord("a") + i) for i in range(26)]
q = deque("a")
ans = []
while q:
    s = q.pop()
    if len(s) == N:
        ans.append(s)
        continue
    for x in c[:c.index(max(s)) + 2]:
        q.append(s + x)
[print(a) for a in sorted(ans)]

Submission status: Accepted

Solution code:
import queue

n = int(input())
alp = "abcdefghijklmn"
q = queue.Queue()
q.put("a")
while not q.empty():
    qi = q.get()
    if len(qi) == n:
        print(qi)
    elif len(qi) < n:
        idx = alp.index(max(qi))
        for i in range(idx+2):
            q.put(qi+alp[i])

Submission status: Accepted

Solution code:
N = int(input())
answers = []

def cal(already_used, answer):
    if len(answer) == N:
        answers.append(answer)
        return

    if len(already_used) == 0:
        plus_1 = 0
    else:
        plus_1 = max(already_used) + 1

    cur_target = already_used + [plus_1]
    for c in cur_target:
        _answer = answer + str(c)
        if c != plus_1:
            _answer = cal(already_used, _answer)
        else:
            _answer = cal(cur_target, _answer)


cal([], "")
for answer in answers:
    for (num, alp) in zip([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]):
        answer = answer.replace(str(num), alp)
    print(answer)

Submission status: Accepted

Solution code:
def dfs(s,n):
    if len(s) == n:
        print(s)
    else:
        for i in range(len(set(s))+1):
            dfs(''.join([s,chr(97+i)]),n)

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/panasonic2020/tasks/panasonic2020_d

n = int(input())

def dfs(s, mx, path):
    if n == len(s):
        path.append(s)
        return
    for i in range(ord('a'), mx + 1):
        if i == mx:
            dfs(s + chr(i), mx + 1, path)
        else:
            dfs(s + chr(i), mx, path)

path = []
dfs('', ord('a'), path)
path.sort()
for ans in path:
    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    if N==1:
        print('a')
        exit()
    d = deque(['a'])
    ans = []
    while d:
        s = d.popleft()
        last = ord(max(s))
        for i in range(97,last+2):
            S = s + chr(i)
            if len(S)==N:
                ans.append(S)
            else:
                d.append(S)
    ans.sort()
    for s in ans:
        print(s)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
N = int(input())
if N == 1:
    print("a")
else:
    S = list("abcdefghij")
    Q = deque([("a",1)])  #文字列、種類
    while len(Q) != 0:
        s,kind = Q.popleft()
        for i in range(kind+1):
            news = s+S[i]
            if len(news) == N+1:
                break
            if len(news) == N:
                print(news)
            if i == kind:
                Q.append((news,kind+1))
            else:
                Q.append((news,kind))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

N = int(input())

lst1 = [('a', 0)]
lst2 = []
for i in range(N - 1):
    if i % 2 == 0: #lst1 -->lst2
        lst2 = []
        for s, t in lst1:
            last = max(ord(s[i]) - ord('a'), t)
            for j in range(last + 2):
                lst2.append((s + chr(j + ord('a')), max(t, j)))
    else:
        lst1 = []
        for s, t in lst2:
            last = max(ord(s[i]) - ord('a'), t)
            for j in range(last + 2):
                lst1.append((s + chr(j + ord('a')), (max(t, j))))
           
if len(lst2) > len(lst1):
    lst2.sort()
    for s, t in lst2:
        print (s)
else:
    lst1.sort()
    for s, t in lst1:
        print (s)

Submission status: Accepted

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


def main():
    N = int(input())
    dfs('a', N)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

alpha=list('abcdefghijk')

q=deque([])
q.append(alpha[0])

for i in range(1,n):
    qi=deque([])

    while(len(q)>0):
        qij=q.popleft()
        qij_setnum=len(set(list(qij)))
        for j in range(qij_setnum+1):
            qi.append(qij+alpha[j])

    q=qi

q=list(q)
q.sort()
for i in range(len(q)):
    print(q[i])

Submission status: Accepted

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

n = int(input())
s = ["a"]
ord_a = ord("a")
ans = []

def dfs(s):
    if len(s) == n:
        ans.append("".join(s))
        return   
    max_ord = 0
    for i in s:
        max_ord = max(max_ord, ord(i) - ord_a)
    for j in range(max_ord + 2):
        new_s = s[:]
        new_s.append(chr(j + ord_a))
        dfs(new_s)
if n == 1:
    print("a")
    sys.exit()
dfs(s)
ans.sort()
for i in ans:
    print(i)

Submission status: Accepted

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

n = int(input())
s = ["a"]
ord_a = ord("a")
ans = []
from copy import deepcopy
def dfs(s):
    if len(s) == n:
        ans.append("".join(s))
        return   
    max_ord = 0
    for i in s:
        max_ord = max(max_ord, ord(i) - ord_a)
    for j in range(max_ord + 2):
        new_s = deepcopy(s)
        new_s.append(chr(j + ord_a))
        dfs(new_s)
if n == 1:
    print("a")
    sys.exit()
dfs(s)
ans.sort()
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
n = int(input())

L = [0]*n
for i in range(n):
    L[i] = chr(i+ord('a'))
#print(L)_

ans = []
def dfs(A):
    if len(A) == n:
        ans.append(''.join(A))
        return
    for v in L[0:len(set(A))+1]:
        A.append(v)
        dfs(A)
        A.pop()

dfs([])
ans.sort()
for i in range(len(ans)):
    print(ans[i])

Submission status: Accepted

Solution code:
from collections import *

def dfs(now):
    if len(now)==N:
        print(''.join(now))
        return

    for i in range(ord(max(now))-ord('a')+2):
        now.append(alpha[i])
        dfs(now)
        now.pop()

N = int(input())
alpha = 'abcdefghijklmnopqrstuvwxyz'
dfs(deque(['a']))

Submission status: Accepted

Solution code:
import string
N = int(input())
alph = string.ascii_lowercase

ans = []
A = [0]
def search():
    if len(A) == N:
        ans.append(tuple(A))
        return

    mx = max(A)
    for i in range(mx + 2):
        A.append(i)
        search()
        A.pop()
    return

search()

for a in ans:
    print(''.join(alph[i] for i in a))

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:
N=int(input())
def struct(s,i,n,ma):
    if i==n:
        print(s)
    else:
        for j in range(0,ma+2):
            struct(s+chr(ord('a')+j),i+1,n,max(j,ma))
struct('a',1,N,0)

Submission status: Accepted

Solution code:
# パナソニック2020D
import sys
def write(x):
    sys.stdout.write(x)
    sys.stdout.write("\n")
    
from queue import deque
n = int(input())
q = deque([("a", "a")])
while True:
    s, m = q.pop()
    if len(s)==n:
        write(s)
    elif len(s)>=n+1:
        break
    for o in range(ord("a"), ord(m)+2):
        if ord(m)<o:
            m = chr(o)
        q.appendleft((s + chr(o), m))

Submission status: Accepted

Solution code:
# パナソニック2020D
import sys
write = lambda x: sys.stdout.write(x+"\n")
from queue import deque
n = int(input())
q = deque([("a", "a")])
while True:
    s, m = q.pop()
    if len(s)==n:
        write(s)
    elif len(s)>=n+1:
        break
    for o in range(ord("a"), ord(m)+2):
        if ord(m)<o:
            m = chr(o)
        q.appendleft((s + chr(o), m))

Submission status: Accepted

Solution code:
# パナソニック2020D
from queue import deque
n = int(input())
q = deque([("a", "a")])
while True:
    s, m = q.pop()
    if len(s)==n:
        print(s)
    elif len(s)>=n+1:
        break
    for o in range(ord("a"), ord(m)+2):
        if ord(m)<o:
            m = chr(o)
        q.appendleft((s + chr(o), m))

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 10
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===
def main():
    n = ni()

    char = [chr(i) for i in range(97, 97 + 26)]
    d = [0 for _ in range(11)]
    d[0] = 1

    def printer(d):
        s = ""
        for num in d:
            if num != 0:
                s = char[num - 1] + s

        if len(s) == n:
            print(s)
        return

    while d[n] == 0:
        printer(d)

        d[0] += 1

        for i in range(10):
            if d[i] > max(d[i + 1:]) + 1:
                d[i] = 1
                d[i + 1] += 1


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def bfs(N):
    q = deque(["a"])

    res = []
    while q:
        s = q.popleft()
        if len(s) == N:
            res.append(s)
            continue

        n = len(set(list(s)))
        for i in range(n + 1):
            c = chr(ord("a") + i)
            q.append(s + c)

    return res


def main():
    N = int(input())
    ans = bfs(N)
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from collections import deque


def input():
    return sys.stdin.readline().strip()


MOD = 10 ** 9 + 7
sys.setrecursionlimit(20000000)
INF = float("inf")


def main():
    N = int(input())
    q = deque(["a"])
    cnt = 1
    answer = []
    while cnt <= N:
        d = q.popleft()

        if len(d) == N:
            answer.append(d)
        cnt = max(cnt, len(d))
        b = 0
        for i in range(cnt):
            if ord(d[i]) > b:
                b = ord(d[i])

        for i in range(ord("a"), b + 2):
            q.append(d + chr(i))
    answer.sort()
    print(*answer, sep="\n")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())

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


s = {}
s["a"] = 0
ans = [s]

for i in range(2, n+1):
    new_s = {}
    for t in s.items():
        for j in range(1, i):
            if t[0][-1] == w[j-1]:
                for k in range(t[1] + 2):
                    new_s[t[0] + w[k]]  = max(t[1], k)

    ans.append(new_s)
    s = new_s

answer = sorted(ans[n-1].keys())

for x in answer:
    
    print(x)

Submission status: Accepted

Solution code:
def dfs(s, mx):#sに対して、sと同じかsの一つ辞書順で後の文字列を追加する関数
    if len(s) == n:
        print(s)
        return
    for c in range(ord("a"), mx + 1):#
        dfs(s + chr(c), mx + 1 if c == mx else mx)#再帰的に一つ追加する

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def dfs(s,mxs):
    if len(s)==n:
        print(s)
    else:
        for i in range(ord('a'),ord(mxs)+1):
            dfs(s+chr(i),chr(i+1) if i ==ord(mxs) else mxs)

def main():
    global n
    n=int(input())
    dfs('','a')
    

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
def generator(n):
    if n == 1:
        yield [0]
    else:
        for A in generator(n - 1):
            for i in range(max(A) + 2):
                A.append(i)
                yield A
                A.pop()


n = int(input())
for A in generator(n):
    print(''.join([chr(a + 97) for a in A]))

Submission status: Accepted

Solution code:
# Panasonic2020 D - String Equivalence
def generator(n):
    if n == 1:
        yield [0]
    else:
        for A in generator(n - 1):
            for i in range(max(A) + 2):
                A.append(i)
                yield A
                A.pop()


n = int(input())
for A in generator(n):
    s = ''
    for a in A:
        s += chr(a + 97)
    print(s)