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: Time Limit Exceeded

Solution code:
from sys import stdin,setrecursionlimit
import string
from itertools import product
setrecursionlimit(10**7)
n = int(stdin.readline().rstrip())
alpha = "abcdefghijklmn"
kouho = set("")
#0~9までの数字を列挙
li = [str(i) for i in range(n)]
#重複ありで全探索
lin = list(product(li, repeat=n))
for i in lin:
    #数列にする
    s = ''.join(map(str, i))
    point = 0
    moto = s[:]
    while True:
        flag = False
        for j in s:
            #数字が出てきたらアルファベットに置換する
            if j in "0123456789":
                s = s.replace(j,alpha[point])
                point += 1
                flag = True
                break
        #すべての文字列が置換されたらbreak
        if flag == False:
            break
    #重複してたら候補に入れない
    kouho.add(s)
kouho = list(kouho)
kouho.sort()
for i in kouho:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[]
B=[[] for i in range(N+1)]
B[0].append("")
for i in range(N):
    A.append(chr(97+i))
    for j in B[i]:
        for k in A:
            B[i+1].append(j+k)
for i in B[N]:
    count=ord("a")
    yes=1
    for j in range(N):
        if ord(i[j])<=count+1:
            count=max(count,ord(i[j]))
        else:
            yes=0
            continue;
    if yes==1:
        print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
import itertools
from collections import deque
ans = deque()
l = []
r = deque()
for i in range(N):
  l.append(chr(97+i))
  r.append(l.copy())
  
p = [[] for _ in range(N)]
p[0].append('a')
for i in range(1,N):
  for j in r[i]:
    for k in p[i-1]:
      p[i].append(k+j)
      
p = p[-1]
for i in p:
  a = [0]*10
  for j in i:
    x = ord(j)-97
    a[x]=1
    if x==0:
      judge = True
      continue
    elif a[x-1]==1:
      judge = True
      continue
    else:
      judge = False
      break
  if judge:
    ans.append(i)
ans = set(ans)
ans = sorted(list(ans))
for a in ans:
  print(a)

Submission status: Time Limit Exceeded

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

def L(n):
  if n == 1:
    return l
  else:
    m = len(L(n-1))
    N = []
    for i in range(m):
      M = []
      k = len(set(L(n-1)[i]))
      for j in range(k+1):
        M.append(L(n-1)[i]+A[j]) 
      N += M
    return N

for s in range(len(L(n))):
  print(L(n)[s])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[]
B=[[] for i in range(N+1)]
B[0].append("")
for i in range(N):
    A.append(chr(97+i))
    for j in B[i]:
        for k in A:
            B[i+1].append(j+k)
for i in B[N]:
    count=ord("a")
    yes=1
    for j in range(N):
        if ord(i[j])<=count+1:
            count=max(count,ord(i[j]))
        else:
            yes=0
    if yes==1:
        print(i)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations

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

for p in permutations(range(N)):
    D = [-1] * N
    A = []
    for i in range(N):
        for j in range(N):
            if D[j] < p[i]:
                D[j] = p[i]
                A.append(j)
                break
    ans.append(tuple(A))

ans = sorted(set(ans))

for a in ans:
    s = ''.join(map(lambda x:chr(x+97),a))
    print(s)

Submission status: Time Limit Exceeded

Solution code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

N = INT()

import itertools
import collections

alphabet = 'abcdefghijklmnopqrstuvwxyz'

a_set = set()
for w in itertools.product(range(N), repeat=N):
    # print(w, end=", ")
    seen = {}

    i = 0
    for l in w:
        if l not in seen.keys():
            seen[l] = i
            i += 1

    # print(seen, end="")

    ans = [0] * N

    for i in range(len(ans)):
        ans[i] = seen[w[i]]

    if tuple(ans) not in a_set:
        print("".join([alphabet[i] for i in ans]))
        a_set.add(tuple(ans))

Submission status: Time Limit Exceeded

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


def solve(N: int):
    alph = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
    print("a" * N)
    for i in range(N - 1, 0, -1):
        s = ("a" * i) + "b"
        if i == N - 1:
            print(s)
            continue
        r_len = N - 1 - i
        for j in range((r_len + 1) ** r_len + 1):
            if j == (r_len + 1) ** r_len:
                print(s, end="")
                for l in range(r_len):
                    print(alph[l+2], end="")
                print()
                continue
            nokori = j
            s2 = s
            prev_val = set()
            for k in range(r_len):
                if k == r_len - 1:
                    val = nokori
                else:
                    val = nokori // ((r_len + 1) ** (r_len - k - 1))
                prev_val.add(val)
                nokori = nokori % ((r_len + 1) ** (r_len - k - 1))
                s2 += alph[val]
                if val >= 3 and not val - 1 in prev_val:
                    break
            else:
                print(s2)
    # if N >= 3:
    #     for i in range(N):
    #         print(alph[i], end="")
    #     print()



# 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: Time Limit Exceeded

Solution code:
x = [0,1,2,3,4,5,6,7,8,9]
n = int(input())
now = [0,0,0,0,0]
if n == 1:
    print("a")
elif n == 2:
    print("aa")
    print("ab")
elif n == 3:
    for a in range(3):
        for b in range(3):
            for c in range(3):
                y = [a,b,c]
                if sorted(set(y), key=y.index) == x[:len(set(y))]:
                    print("".join([chr(z+97) for z in y]))
                    if len(set(y)) == n:
                        exit()
elif n == 4:
    for a in range(n):
        for b in range(n):
            for c in range(n):
                for d in range(n):
                    y = [a,b,c,d]
                    if sorted(set(y), key=y.index) == x[:len(set(y))]:
                        print("".join([chr(z+97) for z in y]))
                        if len(set(y)) == n:
                            exit()
elif n == 5:
    for a in range(n):
        for b in range(n):
            for c in range(n):
                for d in range(n):
                    for e in range(n):
                        y = [a,b,c,d,e]
                        if sorted(set(y), key=y.index) == x[:len(set(y))]:
                            print("".join([chr(z+97) for z in y]))    
                            if len(set(y)) == n:
                                exit()   
                                         
elif n == 6:
    for a in range(n):
        for b in range(n):
            for c in range(n):
                for d in range(n):
                    for e in range(n):
                        for f in range(n):

                            y = [a,b,c,d,e,f]
                            if sorted(set(y), key=y.index) == x[:len(set(y))]:
                                print("".join([chr(z+97) for z in y]))          
                                if len(set(y)) == n:
                                    exit()          
elif n == 7:
    for a in range(n):
        for b in range(n):
            for c in range(n):
                for d in range(n):
                    for e in range(n):
                        for f in range(n):
                            for g in range(n):
                                y = [a,b,c,d,e,f,g]
                                if sorted(set(y), key=y.index) == x[:len(set(y))]:
                                    print("".join([chr(z+97) for z in y]))        
                                    if len(set(y)) == n:
                                        exit()            
elif n == 8:
    for a in range(n):
        for b in range(n):
            for c in range(n):
                for d in range(n):
                    for e in range(n):
                        for f in range(n):
                            for g in range(n):
                                for h in range(n):
                                    y = [a,b,c,d,e,f,g,h]
                                    if sorted(set(y), key=y.index) == x[:len(set(y))]:
                                        print("".join([chr(z+97) for z in y]))                  
                                        if len(set(y)) == n:
                                            exit()      
elif n == 9:
    for a in range(n):
        for b in range(n):
            for c in range(n):
                for d in range(n):
                    for e in range(n):
                        for f in range(n):
                            for g in range(n):
                                for h in range(n):
                                    for i in range(n):
                                        y = [a,b,c,d,e,f,g,h,i]
                                        if sorted(set(y), key=y.index) == x[:len(set(y))]:
                                            print("".join([chr(z+97) for z in y]))          
                                            if len(set(y)) == n:
                                                exit()         
elif n == 10:
    for a in range(n):
        for b in range(n):
            for c in range(n):
                for d in range(n):
                    for e in range(n):
                        for f in range(n):
                            for g in range(n):
                                for h in range(n):
                                    for i in range(n):
                                        for j in range(n):
                                            y = [a,b,c,d,e,f,g,h,i,j]
                                            if sorted(set(y), key=y.index) == x[:len(set(y))]:
                                                print("".join([chr(z+97) for z in y]))               
                                                if len(set(y)) == n:
                                                    exit()

Submission status: Time Limit Exceeded

Solution code:
def chg(X, n):
    X_dumy = X
    out = ''
    while X_dumy>0:
        out = str(X_dumy%n)+out
        X_dumy = int(X_dumy/n)
    return out

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

import sys
if N==1:
    print("a")
    sys.exit()
for i in range(10**7):
    mm = str(chg(i,N)).zfill(N)
    cnt = -1
    li1 = []
    for j in range(N):
        a = int(mm[j])
        if str(a) in li1:
            continue
        elif a-1==cnt:
            cnt += 1
            li1.append(str(a))
            continue
        break
    else:
        for j in range(N):
            print(li[int(mm[j])],end="")
        print()
    # print(mm)

# print(10*9*8*7*6*5*4*3*2)

Submission status: Time Limit Exceeded

Solution code:
#!python3.4.3

import itertools

def report(x):
  tmp = []
  r = ''
  for c in x:
    if c not in tmp:
      tmp.append(c)
    r += str(tmp.index(c))

  return r

n = int(input())
s = ['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']
report_list = []

for i, v in enumerate(itertools.product(s[0:n], repeat=n)):
  temp = ''.join(v)
  
  if report(temp) not in report_list:
    report_list.append(report(temp))
    print(temp)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())

moji = [
    "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"
]
moji = moji * 10
moji = sorted(moji)

l = []
for v in itertools.permutations(moji, n):
    l.append(v)

ll = []
ll = sorted(set(l))

for i in range(n):
    print((','.join(ll[i]).replace(",", "")))

Submission status: Time Limit Exceeded

Solution code:
import itertools
import string

def main():
    length = int(input())
    seq = (chr(i) for i in range(97, 97+length))

    l = list(itertools.product(seq, repeat=length))
    s = ""

    for i, val in enumerate(l):
        minchar = 'a'
        fl = True
        if val[0] != 'a':
            continue
        for j in range(len(val)-1):
            if val[j] > val[j+1]:
                if ord(val[j+1]) - ord(minchar) > 0:
                    fl = False
                    break
            if ord(val[j+1]) - ord(minchar) > 0:
                if ord(val[j+1]) - ord(minchar) > 1:
                    fl = False
                    break
                else:
                    minchar = val[j+1]
        if fl:
            s += ("".join(val) + "\n")

    print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!python3.4.3

import itertools

def report(x):
  tmp = []
  r = ''
  for c in x:
    if c not in tmp:
      tmp.append(c)
    r += str(tmp.index(c))

  return r

n = int(input())
s = ['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']
report_list = []

for v in itertools.product(s, repeat=n):
    temp = ''.join(v)
    if report(temp) not in report_list:
      report_list.append(report(temp))
      print(temp)

Submission status: Time Limit Exceeded

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

groups = []

def dfs(s, n):
    if n == N:
        bits = [-1] * N
        for i, ss in enumerate(s):
            if bits[i] != -1:
                continue
            for ii in range(i+1, N):
                if s[ii] == ss:
                    bits[ii] = i
        if bits not in groups:
            groups.append(bits)
            print(s)
        return
    # for ns in 'abcdefghijklmnopqrstuvwxyz'[:n+1]:
    for ns in 'abcdefghij'[:n+1]:
        dfs(s+ns, n+1)

dfs('', 0)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
import itertools
from collections import deque
ans = deque()
l = []
for i in range(N):
  l.append(chr(97+i))
  for x in itertools.product(l, repeat=N):
    f = [0]*10
    x = list(x)
    judge = True
    k = False
    for w in x:
      w = ord(w)-97
      f[w]=1
      if w==0:
        continue
      elif f[w-1]==1:
        continue
      else:
        k = True
        
    if k:
      continue
    else:
      ans.append(''.join(x))
ans = set(ans)
ans = sorted(list(ans))
for a in ans:
  print(a)

Submission status: Time Limit Exceeded

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

num = "abcdefghijklmnopqrstuvwxyz"
dn = ""
for i in range(n):
    dn += 'z'

def sep(x, y):
    for i in range(len(x) - 1):
        for j in range(i + 1, len(x)):
            if x[i] == x[j]:
                if y[i] != y[j]: return False
            else:
                if y[i] == y[j]: return False
    return True

s = []
for i in range(n):
    for j in range(n - i):
        s.append(num[j])
for v in itertools.permutations(s, n):
    u = ""
    for t in v:
        u += t
    pre.append(u)
ans = list(set(pre))
ans.sort()
for x in range(len(ans) - 1):
    for y in range(x + 1, len(ans)):
        if ans[x] == dn or ans[y] == dn: continue
        if sep(ans[x], ans[y]):
            ans[y] = dn
for x in ans:
    if x == dn: continue
    print(x)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import string
import itertools
 
n = int(input())
num = [i for i in range(11)]
s = string.ascii_lowercase
#lis = list(itertools.combinations_with_replacement(num[0:n-1], n-1))
lis = list(itertools.product(num[0:n-1], repeat=n-1))
 
if n != 1:
    for li in lis:
        #print(li)
        for i in range(n-2):
            if (li[i+1] - li[i] >= 2 and li[i+1]-1 not in li[:i+1]) or li[0] >= 2:
                break
        else:
            print('a', end='')
            for i in range(n-1):
                print(s[li[i]], end='')
            print('')
 
print(s[:n])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())-1

dic={}
ans=[]
#N=1
dic['a']=1
ans.append('a')

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


for i in range(N):
    ans_temp = ans
    dic_temp = dic
    #init for N
    ans = []
    dic={}
    #left a
    for j in ans_temp:
        new_j = 'a'+ j
        ans.append(new_j)
        dic[new_j]=dic_temp[j]
    # right append
    for l in ans_temp:
        num_alp = dic_temp[l]
        for m in range(num_alp+1):
            new_l = l+alp[m]

            if new_l not in ans:
                ans.append(new_l)
                if m==num_alp:
                    dic[new_l]=dic_temp[l]+1
                else:
                    dic[new_l]=dic_temp[l]

        
    
        
        

for i in ans:
    print(i)

Submission status: Time Limit Exceeded

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

groups = []

def dfs(s, n):
    if n == N:
        bits = [-1] * N
        for i, ss in enumerate(s):
            if bits[i] != -1:
                continue
            for ii in range(i+1, N):
                if s[ii] == ss:
                    bits[ii] = i
        if bits not in groups:
            groups.append(bits)
            print(s)
        return
    for ns in 'abcdefghijklmnopqrstuvwxyz'[:n+1]:
        dfs(s+ns, n+1)

dfs('', 0)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
import itertools
from collections import deque
ans = deque()
l = []
for i in range(N):
  l.append(chr(97+i))
  for x in itertools.product(l, repeat=N):
    f = [0]*10
    x = list(x)
    judge = True
    k = False
    for w in x:
      w = ord(w)-97
      f[w]=1
      if sum(f[:w])>=w:
        continue
      else:
        k = True
        
    if k:
      continue
    for j,y in enumerate(x[:i+1]):
      if y in l[:j+1]:
        continue
      else:
        judge=False
        break
    if judge:
      ans.append(''.join(x))
  
ans = set(ans)
ans = sorted(list(ans))
for a in ans:
  print(a)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import string

length = int(input())
seq = (chr(i) for i in range(97, 97+length))

l = list(itertools.product(seq, repeat=length))

s = ""

for i, val in enumerate(l):
    minchar = 'a'
    fl = True
    if val[0] != minchar:
        continue
    for j in range(len(val)-1):
        if val[j] > val[j+1]:
            if ord(val[j+1]) - ord(minchar) > 0:
                fl = False
                break
        if ord(val[j+1]) - ord(minchar) > 0:
            if ord(val[j+1]) - ord(minchar) > 1:
                fl = False
                break
            else:
                minchar = val[j+1]
    if fl:
        s += ("".join(val) + "\n")

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
# import numpy as np
# readline = sys.stdin.readline
# readlines = sys.stdin.readlines

# S = input()
# N, M = map(int, input().split())
# As = [int(input()) for _ in range(N)]
# nums = list(map(int, input().split()))
# nums_2d = [list(map(int, input().split())) for _ in range(N)]


def main():
    solve()


def solve():
    N = int(input())

    use = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
    ans = ['a'] * N
    cand = [use[:i+1] for i in range(N)]

    app = []
    flags = [True] * N
    apnd(ans, 0, cand, app, flags)

    for ap in app:
        last = 0
        flg = True

        for s in ap:
            idx = use.index(s)
            if idx - last > 1:
                flg = False
                break
            last = max(last, idx)
        if flg:
            print(ap)


def apnd(s, n, cand, app, flags): # 文字列sのn文字目を変換
    if n >= len(cand):
        app.append(''.join(s))
        return app

    ls = cand[n]
    for i, c in enumerate(ls):
        if flags[n]:
            s[n] = c
            app = apnd(s, n+1, cand, app, flags)
        if c not in s:
            flags[n] = False
    flags[n] = True

    return app


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
# import numpy as np
# readline = sys.stdin.readline
# readlines = sys.stdin.readlines

# S = input()
# N, M = map(int, input().split())
# As = [int(input()) for _ in range(N)]
# nums = list(map(int, input().split()))
# nums_2d = [list(map(int, input().split())) for _ in range(N)]


def main():
    solve()


def solve():
    N = int(input())

    use = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
    ans = ['a'] * N
    cand = [use[:i+1] for i in range(N)]

    app = []
    apnd(ans, 0, cand, app)

    for ap in app:
        last = 0
        flg = True

        for s in ap:
            idx = use.index(s)
            if idx - last > 1:
                flg = False
                break
            last = max(last, idx)
        if flg:
            print(ap)


def apnd(s, n, cand, app): # 文字列sのn文字目を変換
    if n >= len(cand):
        app.append(''.join(s))
        return app

    ls = cand[n]
    for i, c in enumerate(ls):
        s[n] = c
        app = apnd(s, n+1, cand, app)

    return app


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

n1 = 0
for i in range(n):
    n1 += (n-1)*(10**i)

for i in range(n1 + 1):
    b = list(map(int, list( str(i).zfill(n) )))
    c = [j < n for j in b]
    if not (all(c)):
        continue
    else:
        e = set()
        f = True
        g = 0
        for j in b:
            if j not in e and g == j:
                e.add(g)
                g += 1
            elif j in e:
                continue
            else:
                f = False
                break
        if f:
            d = [a1[j] for j in b]
            print("".join(d))

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
N = int(input())

alphabets = list('abcdefghijklmn')[:10]
for i in product(
        alphabets[:1],
        alphabets[:2],
        alphabets[:3],
        alphabets[:4],
        alphabets[:5],
        alphabets[:6],
        alphabets[:7],
        alphabets[:8],
        alphabets[:9],
        alphabets[:10],
):
    print(''.join(i))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
l = [i for i in range(1, n+1)]
alpha_dict = {}
for i,c in enumerate(range(ord('A'),ord('Z')+1)):
  alpha_dict[i+1] = chr(c)

ans = set()
for v in itertools.product(range(1, n+1), repeat=n):
  m = min(v)
  used = []
  dic = {i:0 for i in range(n+1)}
  tmp = ""
  for u in v:
    if dic[u] == 0:
      dic[u] = len(used)+1
      used += [u]
      tmp += str(alpha_dict[dic[u]]).lower()
    else:
      tmp += str(alpha_dict[dic[u]]).lower()
  ans.add(tmp)

ans = sorted(list(ans))
for v in ans:
  print(v)

Submission status: Time Limit Exceeded

Solution code:
import string


def canonical(s):
    m = {}
    it = iter(string.ascii_lowercase)
    res = ""
    for c in s:
        if c not in m:
            m[c] = next(it)
        res += m[c]
    return res


def main():
    import string
    N = int(input())
    seen = set()
    ASCII = list(string.ascii_lowercase)
    def rec(n, l):
        if n==N:
            s = "".join(l)
            can = canonical(s)
            if can not in seen:
                seen.add(can)
        else:
            for c in ASCII[:n+1]:
                l.append(c)
                rec(n+1, l)
                l.pop()
    rec(0, [])
    ans = sorted(seen)
    print("\n".join(ans))


if __name__ == '__main__':
    # print(canonical("aacc"))
    # print(canonical("aacb"))
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7


s = 'abcdefghijklmnop'
N = I()
s = s[:N]

from itertools import product
seen = set()
for x in product(s,repeat=N):
    d = dict()
    pat = ''
    i = 1
    for n in x:
        if d.get(n) is None:
            d[n] = str(i)
            pat += str(i)
            i += 1
        else:
            pat += d[n]
    if pat in seen:
        continue
    print(''.join(x))
    seen.add(pat)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

from string import ascii_lowercase

from itertools import product

codes = set()
ans = []

def toCode(s):
    code = ''
    used = {}
    idx = 0
    for c in s:
        if c not in used.keys():
            used[c] = idx
            idx += 1
            code += str(used[c])
        else:
            code += str(used[c])
    return code

for k in product(ascii_lowercase[:N], repeat=N):
    s = ''.join(k)
    c = toCode(s)
    # print(c)
    if c not in codes:
        ans.append(s)
        codes.add(c)

# print(codes)

for i in sorted(ans):
    print(i)
# print(ans)

Submission status: Time Limit Exceeded

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

groups = []

def dfs(s, n):
    if n == N:
        bits = [-1] * N
        for i, ss in enumerate(s):
            if bits[i] != -1:
                continue
            for ii in range(i+1, N):
                if s[ii] == ss:
                    bits[ii] = i
        if bits not in groups:
            groups.append(bits)
            print(s)
        return
    for ns in 'abcdefghijklmnopqrstuvwxyz':
        dfs(s+ns, n+1)

dfs('', 0)

Submission status: Time Limit Exceeded

Solution code:
def issame(s, t):
    for i in range(len(s)-1):
        for j in range(i+1, len(s)):
            if s[i]==s[j] and t[i] != t[j]:
                return False
            if s[i]!=s[j] and t[i] == t[j]:
                return False
    return True


def hassame(s, l):
    for t in l:
        if issame(s, t):
            return True
    return False


def main():
    import string
    N = int(input())
    ans = []

    ASCII = list(string.ascii_lowercase)
    def rec(n, l):
        if n==N:
            s = "".join(l)
            if not hassame(s, ans):
                ans.append("".join(l))
        else:
            for c in ASCII[:n+1]:
                l.append(c)
                rec(n+1, l)
                l.pop()
    rec(0, [])
    print("\n".join(ans))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
anstmp = ["a"]
cnt = 0
for i in anstmp:
  t = ord(i[-1])-95
  if cnt > 1000000:
    break
  for j in range(1,t+1):
    anstmp.append(i+chr(j+96))
  cnt += 1
ans = set()
for i in anstmp:
  if len(i) == n:
    ans.add(i)
ans = list(ans)
ans.sort()
print(*ans,sep="\n")

Submission status: Accepted

Solution code:
N = int(input())

def aa(s, n, t):
    if n == N:
        print(s)
        return
    for i in range(t):
        aa(s+chr(97+i), n+1, t)
    aa(s+chr(97+t), n+1, t+1)
    return


aa("a", 1, 1)

Submission status: Accepted

Solution code:
n = int(input())

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


dfs("", ord("a")-1)

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[70]:


N = int(input())


# In[71]:


num2alpha = lambda c: chr(c%26+97)


# In[72]:


def dfs(s,n,cnt):
    if n == 0:
        print(s)
        return
    for i in range(cnt+2):
        if i == cnt+1:
            dfs(s+num2alpha(i),n-1,cnt+1)
        else:
            dfs(s+num2alpha(i),n-1,cnt)
dfs("a",N-1,0)


# In[ ]:

Submission status: Accepted

Solution code:
n = int(input())
v = 'abcdefghij'
def dfs(s):
  if len(s) == n:
    print (s)
    return
  for i in range(len(set(s))+1):
    dfs(s+v[i])
dfs('a')

Submission status: Accepted

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

w = []
def dfs(s,t):
  if len(s) == n:
    w.append(s)
    return
  u = len(set(s))
  for i in range(u+1):
    dfs(s+v[i],u+1)

dfs('a',1)

for i in w:
  print (i)

Submission status: Accepted

Solution code:
N=int(input())
S='abcdefghijklmnopqrstuvwxyz'
A=['a']
for _ in range(N-1):
    now=[]
    for a in A:
        b=sorted(list(a))[-1]
        bi=S.index(b)
        for i in range(bi+2):
            now.append(a+S[i])
    A=now.copy()
A.sort()
for a in A:
    print(a)

Submission status: Accepted

Solution code:
from collections import deque
def main(): 
    n = int(input())
    dq = deque([])
    dq.append([0])
    for _ in range(n-1):
        l = len(dq)
        for __ in range(l):
            s = dq.popleft()
            ls = len(set(s))
            for i in range(ls+1):
                dq.append(s+[i])
    
    for s in dq:
        s = list(map(lambda x: chr(x+ord("a")),s))
        print("".join(s))
main()

Submission status: Accepted

Solution code:
#
# panasonic2020b d
#
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """1"""
        output = """a"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """2"""
        output = """aa
ab"""
        self.assertIO(input, output)


def resolve():
    global N
    N = int(input())

    dfs("a", "a")


def dfs(mw, s):
    if len(s) == N:
        print(s)
        return

    for i in range(ord(mw)-ord("a")+2):
        mw = max(mw, chr(ord("a")+i))
        ns = s + chr(ord("a")+i)
        dfs(mw, ns)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
n=int(input())

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

ans=[]

st='a'

temp=0
now=0

def dfs(st,n,now,temp):
  if now==n-1:
    ans.append(st)
  else:
    for f in range(0,temp+2):
      if f==temp+1:
        dfs(st+al[f],n,now+1,temp+1)
      else:
        dfs(st+al[f],n,now+1,temp)
    
dfs(st,n,0,0)

for h in ans:
  print(h)