local function string_sort( str)
local t = { }
for c in str: gmatch "." do
table.insert ( t, c)
end
table.sort ( t)
return table.concat ( t)
end
local function are_anagramic( w1, w2) return string_sort( w1) == string_sort( w2) end
local function remove_once( t, e)
local res = { }
local removed = false
for _, o in ipairs ( t) do
if o ~= e or removed then table.insert ( res, o) else removed = true end
end
return res
end
local function search( words, str)
if # words == 0 and str: len( ) == 0 then return { { } , { } } , true end
for _, w in ipairs ( words) do
local ana = str: sub( 1 , w: len( ) )
if are_anagramic( w, ana) then
local result, success = search( remove_once( words, w) , str: sub( w: len( ) + 1 ) )
if success then
table.insert ( result[ 1 ] , ana)
table.insert ( result[ 2 ] , w)
return result, true
end
end
end
return "huy tam" , false
end
local function main( )
local str1 = io.read ( )
local words = { }
for w in str1: gmatch "%w+" do
table.insert ( words, w)
end
local str2 = io.read ( )
local result, success = search( words, str2)
if success then
print ( table.concat ( result[ 1 ] , " " ) )
print ( table.concat ( result[ 2 ] , " " ) )
else
print ( result)
end
end
main( )
bG9jYWwgZnVuY3Rpb24gc3RyaW5nX3NvcnQoc3RyKQogIGxvY2FsIHQgPSB7fQogIGZvciBjIGluIHN0cjpnbWF0Y2ggIi4iIGRvICAKICAgIHRhYmxlLmluc2VydCh0LCBjKQogIGVuZAogIHRhYmxlLnNvcnQodCkKICByZXR1cm4gdGFibGUuY29uY2F0KHQpCmVuZAoKbG9jYWwgZnVuY3Rpb24gYXJlX2FuYWdyYW1pYyh3MSwgdzIpIHJldHVybiBzdHJpbmdfc29ydCh3MSkgPT0gc3RyaW5nX3NvcnQodzIpIGVuZAoKbG9jYWwgZnVuY3Rpb24gcmVtb3ZlX29uY2UodCwgZSkKICBsb2NhbCByZXMgPSB7fQogIGxvY2FsIHJlbW92ZWQgPSBmYWxzZQogIGZvciBfLCBvIGluIGlwYWlycyh0KSBkbwogICAgaWYgbyB+PSBlIG9yIHJlbW92ZWQgdGhlbiB0YWJsZS5pbnNlcnQocmVzLCBvKSBlbHNlIHJlbW92ZWQgPSB0cnVl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QKCm1haW4oKQ==
stdin
YWIgYWIgYWIgYWIgYWIgYWJhYiBhYmFiIGFiYWIgYWJhYiBhYmFiIGFiIGFiIGFiIGFiIGFiIGFiYWIgYWJhYiBhYmFiIGFiYWIgYWJhYiBhYiBhYiBhYiBhYiBhYiBhYmFiIGFiYWIKYWJhYmFiYWJhYmFiYWJhYmFiYWJhYmFiYWJhYmFiYWFiYmFhYmJhYWJiYWFiYmFhYmJhYWJiYWFiYmFhYmJhYWJiYWFiYmFhYmJhYWJi
ab ab ab ab ab abab abab abab abab abab ab ab ab ab ab abab abab abab abab abab ab ab ab ab ab abab abab
abababababababababababababababaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabb