fork download
  1. import sys
  2.  
  3. base = {}
  4. flag1 = True
  5. flag2 = True
  6. w_to_n = {}
  7. for letter in "oqz":
  8. w_to_n[letter] = 0
  9. for letter in "ij":
  10. w_to_n[letter] = 1
  11. for letter in "abc":
  12. w_to_n[letter] = 2
  13. for letter in "def":
  14. w_to_n[letter] = 3
  15. for letter in "gh":
  16. w_to_n[letter] = 4
  17. for letter in "kl":
  18. w_to_n[letter] = 5
  19. for letter in "mn":
  20. w_to_n[letter] = 6
  21. for letter in "prs":
  22. w_to_n[letter] = 7
  23. for letter in "tuv":
  24. w_to_n[letter] = 8
  25. for letter in "wxy":
  26. w_to_n[letter] = 9
  27.  
  28.  
  29. def sorting(num, base):
  30.  
  31. sorted_by_lenth = []
  32. sorted_by_order = []
  33. sorted_base = sorted(base, key = len)[::-1]
  34. newnum = num
  35.  
  36. def resort_by_lenth(newnum, sorted_base):
  37.  
  38. if sorted_base[0] in newnum:
  39. sorted_by_lenth.append(sorted_base[0])
  40. newnum_lst = list(newnum.partition(sorted_base[0]))
  41. newnum_lst.pop(1)
  42. newnum = "".join(newnum_lst)
  43. sorted_base.pop(0)
  44.  
  45. if len(sorted_base) > 0 and newnum != "":
  46. resort_by_lenth(newnum, sorted_base)
  47. pass
  48.  
  49. resort_by_lenth(newnum, sorted_base)
  50.  
  51. def resort_by_order(num, sorted_by_lenth):
  52.  
  53. for _ in range(len(sorted_by_lenth)):
  54. if num.partition(sorted_by_lenth[_])[0] == '':
  55. num = num[len(sorted_by_lenth[_]):]
  56. sorted_by_order.append(base[sorted_by_lenth[_]])
  57. sorted_by_lenth.pop(_)
  58. break
  59. if len(sorted_by_lenth) > 0:
  60. resort_by_order(num, sorted_by_lenth)
  61. pass
  62.  
  63. resort_by_order(num, sorted_by_lenth)
  64.  
  65. if sorted_by_order != []:
  66. print(" ".join(sorted_by_order))
  67. else: print("No solution.")
  68.  
  69. pass
  70.  
  71.  
  72. for line in sys.stdin:
  73. if line == "-1" or line == "-1\n":
  74. break
  75. if flag1:
  76. flag1 = False
  77. num = line[:-1]
  78. continue
  79. if flag1 == False and flag2:
  80. flag2 = False
  81. counter = int(line)
  82. continue
  83. converted = ""
  84. for _ in (line[:-1]):
  85. converted += str(w_to_n[_])
  86. base[converted] = line[:-1]
  87. for letter in line[:-1]:
  88. converted += str(w_to_n[letter])
  89. counter -= 1
  90. if counter <= 0:
  91. flag1 = True
  92. flag2 = True
  93. sorting(num, base)
  94. base = {}
  95.  
Success #stdin #stdout 0.02s 27712KB
stdin
7325189087
5
it
your
reality
real
our
4294967296
5
it
your
reality
real
our
1234517
6
iad
i
adgk
g
k
is
-1
stdout
reality our
No solution.
i adgk is