from collections import deque

w = input()
ws = input().split()
q = deque([([], w)])
ans = set()
while len(q) > 0:
    path, r = q.popleft()
    if r == '':
        s = ' '.join([p[1] for p in path])
        print(s, len(path))
        exit()
        ans.add(s)
    for i, x in enumerate(ws):
        if r.startswith(x) and not i in set(e[0] for e in path):
            q.appendleft((path + [(i, x)], r[len(x):]))
print(ans.pop() if len(ans) == 1 else "Unsolvable")

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: