Problem:


Score : 400 points

Problem Statement

In this problem, we only consider strings consisting of lowercase English letters.

Strings s and t are said to be isomorphic when the following conditions are satisfied:

|s| = |t| holds.

For every pair i, j, one of the following holds:

s_i = s_j and t_i = t_j.

s_i \neq s_j and t_i \neq t_j.

For example, abcac and zyxzx are isomorphic, while abcac and ppppp are not.

A string s is said to be in normal form when the following condition is satisfied:

For every string t that is isomorphic to s, s \leq t holds. Here \leq denotes lexicographic comparison.

For example, abcac is in normal form, but zyxzx is not since it is isomorphic to abcac, which is lexicographically smaller than zyxzx.

You are given an integer N.
Print all strings of length N that are in normal form, in lexicographically ascending order.

Constraints

1 \leq N \leq 10

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Assume that there are K strings of length N that are in normal form: w_1, \ldots, w_K in lexicographical order.
Output should be in the following format:

w_1
:
w_K

Sample Input 1

1

Sample Output 1

a

Sample Input 2

2

Sample Output 2

aa
ab



Sample input:
1


Sample output:
a

Submission status: Accepted

Solution code:
import copy as cp

alp  ="abcdefghij"

N = int(input())

list_old = [("a",1)]

if N == 1:
    print("a")
else:
    for i in range(N-1):
        list_new = []
        for elem in list_old:
            for j in range(elem[1]+1):
                list_new.append((elem[0]+alp[j],max(j+1, elem[1])))
        list_old = cp.deepcopy(list_new)
    for elem in list_old:
        print(elem[0])

Submission status: Accepted

Solution code:
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(input())
    chars = "abcdefghij"
    que = deque()
    que.append(("a",1))

    while que:
        s, num = que.popleft()

        if len(s) == n:
            print(s)
        else:
            for i in range(num+1):
                ss = s + chars[i]
                if i == num:
                    que.append((ss,num+1))
                else:
                    que.append((ss,num))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

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

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

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

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

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examD()

"""

"""

Submission status: Accepted

Solution code:
n = int(input())

buf = [["a",97]]
a = 97

for i in range(n-1):
    next = []
    for b in buf:
        for j in range(97,b[1]+1):
            next.append([b[0]+chr(j),b[1]])
        next.append([b[0]+chr(b[1]+1),b[1]+1])
    buf = next

ans = [b[0] for b in buf]

ans.sort()
# print(ans)
#
# print(buf)
# print(len(buf))
for a in ans:
    print(a)

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())

d = deque()

d.append(([],0,-1))
dlen = 1
while dlen != 0:
    arr,le,ma = d.popleft()
    dlen -= 1
    if le == N:
        for a in arr:
            print('abcdefghij'[a],end='')
        print('')
        continue
    for i in range(ma+2):
        aarr = arr + [i]
        lle = le + 1
        mma = max(ma,i)
        d.append((aarr,lle,mma))
        dlen += 1

Submission status: Accepted

Solution code:
N = int(input())

def it(remain,n):
    if remain == 0:
        yield tuple()
    else:
        for x in range(n):
            for d in it(remain-1,n):
                yield (x,)+d
        for d in it(remain-1,n+1):
            yield (n,)+d


print(*(''.join(chr(x+ord('a')) for x in d) for d in it(N,0)), sep='\n')

Submission status: Accepted

Solution code:
N = int(input())
ans = []
char = ["a"]


def dfs(idx, c):
    if idx == N:
        ans.append("".join(char))
    else:
        for i in range(c+2):
            nc = chr(ord("a")+i)
            char.append(nc)
            dfs(idx+1, max(i, c))
            char.pop()

dfs(1, 0)
for i in range(len(ans)):
    print(ans[i])

Submission status: Accepted

Solution code:
def toString(s):
    return "".join([chr(x + ord('a')) for x in s])


def func(s, mx, N):
    if len(s) == N:
        print(toString(s))
        s.pop()
        return
    for i in range(mx+2):
        s.append(i)
        func(s, max(mx, i), N)
    s.pop()


def main():
    N = int(input())
    func([0], 0, N)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
seq = []
res = []
def f(n, lim):
    if n == 0:
        res.append(''.join(chr(c+97) for c in seq))
        return
    for i in range(lim):
        seq.append(i)
        f(n-1, lim)
        seq.pop()
    seq.append(lim)
    f(n-1, lim+1)
    seq.pop()

f(int(input()), 0)
print('\n'.join(res))

Submission status: Accepted

Solution code:
N = int(input())

tmp = [[1]]
answer = tmp

for i in range(N - 1):
    tmp = []
    for a in answer:
        max_a = max(a)
        for j in range(1, max_a + 2):
            tmp.append(a + [j])
    answer = tmp

answer.sort()

def alpha(l):
    res = ""
    for ll in l:
        res += chr(ll + ord("a") - 1)
    return res

for a in answer:
    print(alpha(a))

Submission status: Accepted

Solution code:
import sys
range = xrange
input = raw_input

n = int(input())

ans = []

def generate(A, i = 0, j = 0):
    if j == n:
        if all(a!=n for a in A):
            return ans.append(list(A))
        return
    if i == n:
        if all(a!=n for a in A):
            return ans.append(list(A))
        i = 0
        while A[i] != n:
            i += 1
        A[i] = j + 1
        generate(A, i + 1, j + 1)
        A[i] = n
        return
    if A[i] == n:
        A[i] = j
        generate(A, i + 1, j)
        A[i] = n
    generate(A, i + 1, j)

A = [n]*n
A[0] = 0
generate(A, 1, 0)
orda = ord('a')
print '\n'.join(''.join(chr(a + orda) for a in A) for A in sorted(ans))

Submission status: Accepted

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


N = int(input())
L = list("abcdefghij")

def nasu(x, n, e):
  if n == N:
    print(x)
    return
  for i in range(e + 1):
    if i == e:
      nasu(x + L[i], n + 1, e + 1)
    else:
      nasu(x + L[i], n + 1, e)

nasu("", 0, 0)

Submission status: Accepted

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

l=["a"]
for j in range(2,n+1):
    tmp=[]
    for s in l:
        m=len(set(list(s)))
        for i in ll[:m+1]:
            tmp.append(s+i)
    l=tmp

l.sort()
print('\n'.join(map(str,l)))

Submission status: Accepted

Solution code:
n = int(input())
c = 'abcdefghijk'
d = [[0]]
for i in range(n-1):
  pd,d = d,[]
  for j in pd:
    m = max(j)
    for k in range(m+2):
      d.append(j+[k])
for i in d:
  for j in i[:-1]:
    print(c[j],end='')
  print(c[i[-1]])

Submission status: Accepted

Solution code:
letter = ["a","b","c","d","e","f","g","h","i","j","k","l","m"]


class let():
    def __init__(self, a, b):
        self.s = a
        self.n = b

def add(s_list):
    ans = []
    for s in s_list:
        for i in range(s.n):
            ans.append(let(s.s + letter[i], s.n))
        ans.append(let(s.s + letter[s.n], s.n+1))
    
    return ans




N = int(input())


a_list = [let("a", 1)]

for i in range(N-1):
    a_list = add(a_list)

for i in a_list:
    print(i.s)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def main():
    N=ii()
    ABC = 'abcdefghij'
    def rec(s, cur_max, d):
        if d==0:
            print(s)
            return
        for i in range(cur_max+1+1):
            rec(s + ABC[i], max(cur_max, i), d-1)
    rec('a', 0, N-1)




main()

Submission status: Accepted

Solution code:
n = int(input())

ans_b = []
ans_n = ['a']

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

for i in range(1,n):
    ans_b = ans_n.copy()
    ans_n = []
    for j in ans_b:
        plus = 1
        ans_n.append(j+'a')
        while( j.count(s[plus-1]) > 0):
            ans_n.append(j+ s[plus])
            plus += 1

ans = sorted(ans_n)

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
N = int(input())

l = ["a"]

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

while N-1:
  new_l = []
  for item in l:
    s = len(set(list(item)))
    for i in range(s+1):
      new_l.append(item+c[i])
      
  l = new_l
  N -= 1
  
for item in l:
  print(item)

Submission status: Accepted

Solution code:
def sol(k):

    def gen(d, cur, s):
        if d == k:
            yield s
            return
        for i in range(0, cur+1):
            yield from gen(d+1, max(cur, i+1), s+chr(97+i))

    return gen(0, 0, '')

def main():
    k = int(input())
    for x in sol(k):
        print(x)

if __name__ == '__main__':
    main()

Submission status: Accepted

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


def solve(N: int):
    res = []

    def rec(depth, charset, lst):
        if depth >= N:
            res.append("".join(lst))
            return

        valid_chars = [chr(ord("a") + i) for i in range(len(charset) + 1)]

        for c in valid_chars:
            rec(depth + 1, charset | set(c), lst + [c])

    rec(0, set(), [])
    print("\n".join(res))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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())

words = [chr(ord('a')+i) for i in range(10)]
dp = [('', 0)]
for i in range(1, N+1):
    ndp = []
    for w, c in dp:
        for k in range(c+1):
            ndp.append((w+words[k], c+1 if k == c else c))
    dp = ndp
for l in dp:
    print(l[0])

Submission status: Accepted

Solution code:
abc = "abcdefghijklmnopqrstuvwxyz"
n = int(input())

ans = []


def f(s="", x=1):
    global ans
    if len(s) == n:
        ans.append(s)
        return 0
    for i in range(x):
        f(s + abc[i], max(x, i+2))


f()
ans.sort()
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
import copy

N=int(input())
ANS=[]
def next_s(L,x):
    #print(L,x)
    if min(L)!=-1:
        ANS.append(L)
        return
    EMPTY=[]
    for i in range(N):
        if L[i]==-1:
            EMPTY.append(i)

    L[EMPTY[0]]=x

    LEN=len(EMPTY)-1
    for i in range(1<<LEN):
        S=copy.deepcopy(L)
        for j in range(LEN):
            if i & (1<<j)!=0:
                S[EMPTY[j+1]]=x

        #print(i,L,S)

        next_s(S,x+1)

next_s([-1]*N,0)

ANS.sort()
for al in ANS:
    b=[chr(a+97) for a in al]
    print("".join(b))

Submission status: Accepted

Solution code:
n=int(input())
ans=[]
ap=ans.append
def f(s,t):
    if len(s)==n:
        ap(s)
        return
    else:
        for i in range(t):
            f(s+chr(97+i),t)
        else:
            f(s+chr(97+t),t+1)
f("a",1)
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
const = ord('a')

def sub_rekkyo(s, n):
  # s: 先頭何文字か与えられている
  # n: sの中の文字数
  list_sub = [(s+chr(const+i), n) for i in range(n)]
  list_sub.append((s+chr(const+n), n+1))
  return list_sub

list_prev = [('',0)]

for i in range(N):
  list_next = []
  for s, n in list_prev:
    list_next += sub_rekkyo(s,n)
  list_prev = list_next.copy()

for s, n in list_prev:
  print(s)

Submission status: Accepted

Solution code:
n=int(input())
alp="abcdefghijklmno"

def saiki(now,st,used):
    num=now-1
    if num==0:
        print(st)
    else:
        for i in range(used+1):
            if i==len(range(used+1))-1:
                saiki(now-1,st+alp[i],used+1)
            else:
                saiki(now-1,st+alp[i],used)

saiki(n,"a",1)

Submission status: Accepted

Solution code:
import collections
n=int(input())
b=list("abcdefghij")


def func(l, a):
    if n == a:
        for ans in l:
            print(ans)
        return
    newl=[]
    for tmp in l:
        c=collections.Counter(list(tmp))
        for i in range(len(c)+1):
            newl.append(tmp + b[i])
    func(newl, a+1)

func(['a'], 1)

Submission status: Accepted

Solution code:
from collections import deque


def solve(n):
    if n == 1:
        return ["a"]
    alphabet = "abcdefghijkl"
    res_list = []
    queue = deque([("a", 0)])
    while len(queue) > 0:
        p, k = queue.popleft()
        for i in range(k + 2):
            q = p + alphabet[i]
            queue.append((q, max(i, k)))
            if len(q) == n:
                res_list.append(q)
        if len(q) > n:
            break
    return res_list


def main():
    n = int(input())
    res = solve(n)
    for r in res:
        print(r)


def test():
    assert solve(1) == ["a"]
    assert solve(2) == ["aa", "ab"]


if __name__ == "__main__":
    test()
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=[["a",1]]
d=[chr(ord("a")+i)for i in range(10)]
for i in range(n-1):
    t=[]
    for j in s:
        for h in range(j[1]):
            t.append([j[0]+d[h],j[1]])
        t.append([j[0]+d[h+1],j[1]+1])
    s=t.copy()
for i in sorted(s):
    print(i[0])

Submission status: Accepted

Solution code:
def news(moji, kaz):
    kazz=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
    kotae=[]
    for i in range(kaz):
        kotae.append([moji+kazz[i], kaz])
    kotae.append([moji+kazz[kaz], kaz+1])
    return kotae

n=int(input())
now=[['a', 1]]
for i in range(n-1):
    new=[]
    for nw in now:
        new+=news(nw[0], nw[1])
    now=new
lenw=len(now)
for i in range(lenw):
    print(now[i][0])

Submission status: Accepted

Solution code:
n=int(input())
x=["a"]
d="abcdefghij"
i=1
while i<n:
	y=[]
	for t in x:
		for s in d[:len(set(t))+1]:
			y.append(t+s)
	x=y
	i+=1
print("\n".join(x))

Submission status: Accepted

Solution code:
import string

N = int(input())


def solve(n):
    if n == 1:
        return [[0]]
    result = []
    for v in solve(n - 1):
        x = max(v)
        for i in range(x + 2):
            result.append(list(v) + [i])
    return result


def print_strig(result):
    for r in result:
        print("".join(list(map(lambda v: string.ascii_lowercase[v], r))))


print_strig(solve(N))

Submission status: Accepted

Solution code:
N=int(input())
abc="abcdefghijklmn"

def saiki(s,m):
  if len(s)==N:
    print(s)
  else:
    for i in range(m+2):
      if i<m+1:
        saiki(s+abc[i],m)
      else:
        saiki(s+abc[i],m+1)
    
saiki("a",0)

Submission status: Accepted

Solution code:
N = int(input())

ans = []
def dp(arg, level):
    global N
    if len(arg) == N:
        ans.append(arg)
        return
    #
    orda = ord('a')
    for i in range(level):
        dp(arg + chr(orda + i), level)
    dp(arg + chr(orda + level), level + 1)
    return

dp('', 0)
for ansi in ans:
    print(ansi)

Submission status: Accepted

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

N = int(input())
Alp = [chr(i) for i in range(97, 97+26)]

ans = []
def dfs(d, maxi, T):
    if d == N-1:
        ans.append(T)
        return
    for i in range(maxi+2):
        dfs(d+1, max(maxi, i), T + Alp[i])

dfs(0, 0, "a")
print(*ans, sep='\n')

Submission status: Accepted

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

res = []
def fill(K, f):
    if K.count(-1) == 0:
        res.append(K)
        return 
    else:
        idx = []
        for i in range(N):
            if K[i] == -1:
                idx.append(i)
        kc = len(idx)
        for S in range(1, 1<<kc):
            if not S & 1:
                continue
            K2 = K[:]
            for j in range(kc):
                if S & (1<<j):
                    K2[idx[j]] = f
            fill(K2, f+1)
            
            
N = int(readline())
fill([-1]*N, 0)
res.sort()
res = [''.join(map(lambda x: chr(x+97), r)) for r in res]
print('\n'.join(res))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = 'abcdefghij'
    cur = set([(0, '')])
    for i in range(N):
        nxt = set()
        for c, s in cur:
            for m in S[:c]:
                nxt.add((c, s + m))
            nxt.add((c + 1, s + S[c]))
        cur = nxt
    cur = sorted(i[1] for i in cur)
    print('\n'.join(cur))

main()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations,product
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    res = [[[0],0]]
    for i in range(n-1):
        res = [[x+[i],max(i,y)] for (x,y) in res for i in range(y+2)]
    for j,k in res:
        for i in j:
            print(chr(i+ord("a")),end = "")
        print()
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N=int(input())

tmp = [[[0],0]]
for _ in [0]*(N-1):
    tmp2 = []
    for s,m in tmp:
        for c in range(m+1):
            tmp2.append([s+[c],m])
        tmp2.append([s+[m+1],m+1])
    tmp = tmp2
for ans in tmp:
    s = "".join(["abcdefghij"[i] for i in ans[0]])
    print(s)

Submission status: Accepted

Solution code:
n = int(input())
import string
A = [chr(i) for i in range(ord("a"),ord("a")+10)]
B = {}
for i in range(10):
    B[A[i]] = i

def d(s,t,ANS):
    if len(s) == n:
        ANS.append(s)
    else:
        for i in range(t+2):
            d(s + A[i],max(i,t),ANS)
ANS = []

d("",-1,ANS)

ANS.sort()
for i in ANS:
    print(i)

Submission status: Accepted

Solution code:
N = int(input())
pool = [('a',0)]
ord_a = ord('a')
while pool:
    li, m = pool.pop()
    if len(li) == N:
        print(li)
    else:
        pool.append((li+chr(ord_a+m+1),m+1))
        for i in range(m,-1,-1):
            pool.append((li+chr(ord_a+i),m))

Submission status: Accepted

Solution code:
from collections import deque
N=int(input())
if N==1:
    print("a")
    exit()
X=[chr(i+97) for i in range(N)]
#print(X)
Y=[]
q=deque(["a"])
while(len(q)>0):
    r=q.pop()
    k=len(set(r))
    for i in range(k+1):
        p=r+X[i]
        if len(p)==N:
            Y.append(p)
        else:
            q.append(p)
Y.sort()
for line in Y:
    print(line)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def dfs(mx=0,s=[0]):
        if len(s)==n:
            ans.append(s)
            return
        for i in range(mx+2):
            dfs(max(mx,i),s+[i])

    n=II()
    ans=[]
    dfs()
    for ak in ans:
        print("".join(chr(a+97) for a in ak))

main()

Submission status: Accepted

Solution code:
n = int(input())
abc = 'abcdefghijklmnopqrstuvwxyz'
def sub(l, i):
    if len(l) >= n:
        print(l)
        return
    for k in range(i):
        sub(l + abc[k], i)
    sub(l + abc[i], i + 1)

sub('', 0)

Submission status: Accepted

Solution code:
import string
lower = string.ascii_lowercase

n = int(input())

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

ans = ['']

for i in range(n):
    new_list = []
    for word in ans:
        for new_char in lower:
            if new_char in word:
                new_list.append(word + new_char)
            else:
                new_list.append(word + new_char)
                break
    ans = new_list
#    print(new_list)

for word in ans:
    print(word)

Submission status: Accepted

Solution code:
n=int(input())
def dsf(s,k):
    if len(s)==n:
        print(s)
    else:
        for i in range(k):
            dsf(s+chr(i+97),k)
        dsf(s+chr(k+97),k+1)
dsf("",0)

Submission status: Accepted

Solution code:
n=int(input())
l=[["a"],["aa","ab"],[],[],[],[],[],[],[],[]]
for i in range(2,10):
  for j in l[i-1]:
    syurui=len(set(list(j)))
    for h in range(syurui+1):
      l[i].append(j+chr(97+h))
for i in l[n-1]:print(i)

Submission status: Accepted

Solution code:
s = 'abcdefghij'
sg = s.__getitem__


def dfs(ans):
    if len(ans) == n:
        print(''.join(map(sg, ans)))
        return
    for i in range(max(ans) + 2):
        ans.append(i)
        dfs(ans)
        ans.pop()


n = int(input())
dfs([0])

Submission status: Accepted

Solution code:
def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    N = int(input())
    q = deque()
    q.append([0])

    for i in range(N):
        q_new = deque()
        while q:
            A = q.popleft()
            for j in range(1, max(A) + 2):
                A_new = A[:]
                A_new.append(j)
                q_new.append(A_new)
        q = q_new
    while q:
        A = q.popleft()
        ans = [chr(a+96) for a in A[1:]]
        print(''.join(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

def dfs(s, n):
    if len(s) == N:
        return [s]
    ret = []
    for i in range(n):
        ret += dfs(s + chr(i + ord('a')), n)
    ret += dfs(s + chr(n + ord('a')), n+1)
    return ret

for w in dfs('', 0):
    print(w)

Submission status: Accepted

Solution code:
from copy import deepcopy
def main():
    N = int( input())
    if N == 1:
        print('a')
        return
    ANS = [(1, 'a')]
    a = ord('a')
    for i in range(N-1):
        G = deepcopy(ANS)
        ANS = []
        for c, t in G:
            for j in range(c):
                ANS.append((c,t+chr(a+j)))
            ANS.append((c+1, t+chr(a+c)))
    ANS = [ t[1] for t in ANS]
    print('\n'.join(ANS))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

#n,*a = [int(i) for i in read().split()]
#h,w = [int(i) for i in readline().split()]

n = int(input())
c = [chr(ord("a")+i) for i in range(20)]

ans = []
def dfs(x,i,v):
    if v==n:
        ans.append(x)
        return
    #print(x,i,v)
    for j in range(i+1):
        dfs(x+c[j],i,v+1)
    dfs(x+c[i+1],i+1,v+1)


dfs("",-1,0)

print(*sorted(ans),sep="\n")

Submission status: Accepted

Solution code:
N=int(input())

begins=['a']
while(len(begins[0])<N):
    _begins=[]
    for b in begins:
        n=len(set(b))
        for i in range(n+1):
            _begins.append(b+chr(ord('a')+i))
    begins=_begins

for b in sorted(begins):
    print(b)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    def dfs(s):
        if len(s) == n:
            print(s)
            return
        for i in range(len(set(list(s))) + 1):
            dfs(s + chr(ord("a") + i))
    dfs("")
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
def calc(n, L, ma):
    if n == 0: 
        print("".join([chr(a+97) for a in L]))
        return L
    for i in range(ma+2):
        l = calc(n-1, L + [i], max(ma, i))

calc(N, [], -1)

Submission status: Accepted

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

def ans(n):
    if n == 1:
        return ['a']
    ret = []
    pre = ans(n-1)
    for s in pre:
        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())
res = []

def f(frm, l, head):
    if l == 0:
        res.append(head)
    for i in range(l, 0, -1):
        for j in range(frm):
            f(frm, l - i, head + chr(ord('a') + j) * i)
        f(frm + 1, l - i, head + chr(ord('a') + frm) * i)
    
f(0, n, '')
print(*sorted(list(set(res))), sep='\n')

Submission status: Accepted

Solution code:
N = input()

def f(s, c):
  if len(s)>=N:
    print s
  else:
    for i in range(c+1):
      f(s+chr(ord("a")+i), max(i+1, c))
f("", 0)

Submission status: Accepted

Solution code:
string = []
kind = 0

alphabet = "abcdefghijklmno"

def dfs(n):
    global string, kind
    if n == 0:
        print("".join(string))
        return
    for i in range(kind):
        string.append(alphabet[i])
        dfs(n-1)
        string.pop()
    string.append(alphabet[kind])
    kind += 1
    dfs(n-1)
    kind -= 1
    string.pop()

dfs(int(input()))

Submission status: Accepted

Solution code:
n = int(input())
l = [[[],0]]
for i in range(n):
    nxt = []
    for li, ma in l:
        for j in range(ma+1):
            lj = li[:]
            lj.append(j)
            if j == ma:
                nxt.append([lj, ma+1])
            else:
                nxt.append([lj, ma])
    l = nxt

ans = []
aa = ord("a")
for li,_ in l:
    ans.append("".join(map(lambda x: chr(x+aa), li)))
ans.sort()
print(*ans, sep="\n")

Submission status: Accepted

Solution code:

N = int(input())

alp = "abcdefghijklmnopqrstuvwxyz"

ans =  [ ["",0] ]


for i in range(N):

    nex = []

    for s,i in ans:

        for j in range(i+1):

            nex.append([s+alp[j] , max(i,j+1)])

    ans = nex
    #print (ans)

ans.sort()

for s,i in ans:
    print (s)

Submission status: Accepted

Solution code:
n=int(input())
if n==1:
    print('a')
    exit()
ans=['a']
al=list('abcdefghijklmnopqrsruvwxyz')
for i in range(n-1):
    tmp=[]
    for j in ans:
        sn=len(set(j))
        for k in range(sn+1):
            tmp.append(j+al[k])  
    ans=tmp
ans.sort()
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

oa = ord('a')
n = ni()

def dfs(v, m, cur):
    if v == n:
        s = ''.join(map(lambda x: chr(x+oa), cur))
        return [s]
    ret = list()
    for i in range(m):
        ret.extend(dfs(v+1, m, cur + [i]))
    ret.extend(dfs(v+1, m+1, cur + [m]))
    return ret

l = dfs(0, 0, list())

l.sort()
print(*l, sep='\n')