Words and the Periodic Table
Time limit1sMemory limit128 MB
Split each word into element symbols, case-insensitively, choosing the split with the fewest parts, then the lowest atomic-number sum.
- Level
Medium6 of 10
- Topics
- Dynamic programming, String, Hash map
- Solved
- No attempts yet
Problem
Given a periodic table and a list of words, write a program that represents each word as a sequence of element symbols taken from the periodic table.
If there are several possible representations, apply the following rules in order:
- Choose the one that uses the fewest elements.
- If several remain, choose the one with the smallest element mass (the sum of the numbers of the elements used).
If more than one representation still remains after both rules, or if the word cannot be represented by element symbols at all, print Too Obvious.
An element symbol always begins with a single uppercase letter followed by zero or more lowercase letters. Matching a word against element symbols is case-insensitive.
Input
The first line contains the number of test cases.
Each test case is given as follows:
- The first line contains the number of elements .
- The next lines each contain one element symbol.
- The following line contains the number of words .
- The next lines each contain one word.
, , and the length of every word and element symbol are all less than . Elements are given in order of their atomic number, which starts at and increases by .
Output
For each word, print the element symbols that represent it, in order. Wrap each element symbol in square brackets [] so they can be told apart. Use the rules in the statement to pick a single representation. If the word cannot be represented, or if the chosen representation is not unique, print Too Obvious.