fork download
  1. from collections import OrderedDict
  2. import sys
  3.  
  4. sys.setrecursionlimit(3000)
  5.  
  6. def hash_map(word_map, words):
  7. for word in words:
  8. if word[0] not in word_map:
  9. word_map[word[0]] = []
  10. word_map[word[0]].append(word)
  11.  
  12. def recursions(word_map, paswd, output, remember):
  13. flag = 0
  14. if len(paswd) == 0:
  15. return 1
  16. if paswd in remember:
  17. return flag
  18. for char in paswd:
  19. for word in (word_map[char] if char in word_map else []):
  20. if paswd.startswith(word):
  21. output.append(word + " ")
  22. if recursions(word_map, paswd[len(word):], output, remember):
  23. return 1
  24. output.pop()
  25. remember[paswd] = 1
  26. return flag
  27.  
  28. n = int(input())
  29. for p in range(n):
  30. k = int(input())
  31. words = input().split()
  32. paswd = input()
  33. words.sort(key = lambda s: len(s), reverse=True)
  34. word_map = OrderedDict()
  35. hash_map(word_map, words)
  36. output = []
  37. pp = recursions(word_map, paswd, output, {})
  38. if pp:
  39. print("".join(output))
  40. else:
  41. print("WRONG PASSWORD")
Runtime error #stdin #stdout #stderr 0.01s 28384KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 28, in <module>
EOFError: EOF when reading a line