local function add(bag, char)
if bag[char] then
bag[char] = bag[char] + 1
else
bag[char] = 1
end
end
local function remove(bag, char)
if bag[char] and bag[char] >= 1 then
bag[char] = bag[char] - 1
end
end
local function bag(word)
local b = {}
for char in word:gmatch "." do add(b, char) end
return b
end
local state_index = {}
function state_index:process(str2)
for c in str2:gmatch "." do
for i, bag in ipairs(self.bags) do
if bag[c] and bag[c] >= 1 then
remove(bag, c)
self.anagrams[i] = self.anagrams[i] .. c
break
end
end
end
return self
end
function state_index:print()
print(table.concat(self.anagrams, " "))
print(table.concat(self.words, " "))
end
local function init(str1)
local state = {
words = {},
bags = {},
anagrams = {},
}
setmetatable(state, {__index = state_index})
for word in str1:gmatch("%w+") do
table.insert(state.words, word)
table.insert(state.bags, bag(word))
table.insert(state.anagrams, "")
end
return state
end
init(io.read()):process(io.read()):print()
bG9jYWwgZnVuY3Rpb24gYWRkKGJhZywgY2hhcikKICBpZiBiYWdbY2hhcl0gdGhlbgogICAgYmFnW2NoYXJdID0gYmFnW2NoYXJdICsgMQogIGVsc2UKICAgIGJhZ1tjaGFyXSA9IDEKICBlbmQKZW5kCgpsb2NhbCBmdW5jdGlvbiByZW1vdmUoYmFnLCBjaGFyKQogIGlmIGJhZ1tjaGFyXSBhbmQgYmFnW2NoYXJdID49IDEgdGhlbgogICAgYmFnW2NoYXJdID0gYmFnW2NoYXJdIC0gMQogIGVuZAplbmQKCmxvY2FsIGZ1bmN0aW9uIGJhZyh3b3JkKQogIGxvY2FsIGIgPSB7fQogIGZvciBjaGFyIGluIHdvcmQ6Z21hdGNoICIuIiBkbyBhZGQoYiwgY2hhcikgZW5kCiAgcmV0dXJuIGIKZW5kCgpsb2NhbCBzdGF0ZV9pbmRleCA9IHt9CgpmdW5jdGlvbiBzdGF0ZV9pbmRleDpwcm9jZXNzKHN0cjIpCiAgZm9yIGMgaW4gc3RyMjpnbWF0Y2ggIi4iIGRvCiAgICBmb3IgaSwgYmFnIGluIGlwYWlycyhzZWxmLmJhZ3MpIGRvC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