#C6948. Abbreviate Location Names
Abbreviate Location Names
Abbreviate Location Names
In this problem, you are given a message composed of several location names in their long forms, along with a list of available location names and their corresponding abbreviations. For each location name in the message, you must use the shorter of the two strings: the given abbreviation or the original long form (if the abbreviation is not strictly shorter than the long form). Your task is to convert the original message into a new message where every location name is replaced by its shortest representation.
More formally, given two integers and , where indicates the number of location names in the message and indicates the number of available location entries, followed by pairs of strings and and finally a sequence of location names, for each location name use if (where denotes the length of the string), otherwise use . The answer is the resulting sequence joined by spaces.
inputFormat
The input is read from standard input (stdin) and consists of multiple lines:
-
The first line contains two integers and , representing the number of location names in the message and the number of location abbreviation entries, respectively.
-
The next lines each contain two strings: the long form and its abbreviation .
-
The last line contains space-separated strings representing the location names in the message.
outputFormat
Output a single line to standard output (stdout) containing the shortened message. Each location name in the message is replaced by its shortest representation as defined in the problem statement. The resulting location names should be separated by a single space.## sample
5 3
MOUNTAIN MTN
FOREST FOR
RIVER RIV
MOUNTAIN RIVER RIVER FOREST MOUNTAIN
MTN RIV RIV FOR MTN