import scala.
collection.
immutable.
TreeSet
implicit val wordOrdering
= Ordering.
fromLessThan[Word
](_ != _)
def solve
(items
: Set
[Word
], anagrams
: String
) = { def branch
(trial
: String, position
: Int, candidates
: Set
[Word
]): Option
[Seq
[(String, String
)]] = { val label
= anagrams.
substring(position, position + trial.
length) if (trial.
sorted == label.
sorted) { if (candidates.
isEmpty) Option
(Seq
((trial, label
))) .map(newTrial => branch(newTrial, position + trial.length, candidates - newTrial))
.find(_.isDefined).flatten.map((trial, label) +: _)
}
items.view.map(item => branch(item, 0, items - item)).find(_.isDefined).flatten
}
val items
= TreeSet
(readLine
().
split(" ").
map(new Word
(_)) : _*) val anagrams
= readLine
()
solve
(items, anagrams
) match { case None
=> println
("Ne fart") val (items, labels
) = solution.
unzip labels.foreach(s => print(s + " "))
println()
items.foreach(s => print(s + " "))
}
}
aW1wb3J0IHNjYWxhLmNvbGxlY3Rpb24uaW1tdXRhYmxlLlRyZWVTZXQKCm9iamVjdCBNYWluIGV4dGVuZHMgQXBwIHsKICBpbXBsaWNpdCBjbGFzcyBXb3JkKHZhbCByZXByOiBTdHJpbmcpCiAgaW1wbGljaXQgZGVmIHVud29yZCh3OiBXb3JkKSA9IHcucmVwcgogIGltcGxpY2l0IHZhbCB3b3JkT3JkZXJpbmcgPSBPcmRlcmluZy5mcm9tTGVzc1RoYW5bV29yZF0oXyAhPSBfKQoKICBkZWYgc29sdmUoaXRlbXM6IFNldFtXb3JkXSwgYW5hZ3JhbXM6IFN0cmluZykgPSB7CiAgICBkZWYgYnJhbmNoKHRyaWFsOiBTdHJpbmcsIHBvc2l0aW9uOiBJbnQsIGNhbmRpZGF0ZXM6IFNldFtXb3JkXSk6IE9wdGlvbltTZXFbKFN0cmluZywgU3RyaW5nKV1dID0gewogICAgICB2YWwgbGFiZWwgPSBhbmFncmFtcy5zdWJzdHJpbmcocG9zaXRpb24sIHBvc2l0aW9uICsgdHJpYWwubGVuZ3RoKQogICAgICBpZiAodHJpYWwuc29ydGVkID09IGxh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