508 Morse Mismatches

Samuel F. B. Morse is best known for the coding scheme that carries his name. Morse code is still used in international radio communication. The coding of text using Morse code is straightforward. Each character (case is insignificant) is translated to a predefined sequence ofditsanddahs(the elements of Morse code). Dits are represented as periods (“.”) and dahs are represented as hyphens or minus signs (“-”). Each element is transmitted by sending a signal for some period of time. A dit is rather short, and a dah is, in perfectly formed code, three times as long as a dit. A short silent space appears between elements, with a longer space between characters. A still longer space separates words. This dependence on the spacing and timing of elements means that Morse code operators sometimes do not send perfect code. This results in difficulties for the receiving operator, but frequently the message can be decoded depending on context.

In this problem we consider reception of words in Morse code without spacing between letters. Without the spacing, it is possible for multiple words to be coded the same. For example, if the message “dit dit dit” were received, it could be interpreted as “EEE”, “EI”, “IE” or “S” based on the coding scheme shown in the sample input. To decide between these multiple interpretations, we assume a particular context by expecting each received word to appear in a dictionary.

For this problem your program will read a table giving the encoding of letters and digits into Morse code, a list of expected words (context), and a sequence of words encoded in Morse code (morse). These morse words may be flawed. For eachmorseword, your program is to determine the matching word fromcontext, if any. If multiple words fromcontextmatchmorse, or if no word matches perfectly, your program will display the best matching word and a mismatch indicator.

If a single word fromcontextmatchesmorseperfectly, it will be displayed on a single line, by itself.If multiplecontextwords exist for a givenmorse, the first matching word will be displayed followed by an exclamation point (“!”).

We assume only a simple case of errors in transmission in which elements may be either truncated from the end of amorseword or added to the end of amorseword. When no perfect matches formorseare found, display the word fromcontextthat matches the longest prefix ofmorse, or has the fewest extra elements beyond those inmorse. If multiple words incontextmatch using these rules, any of these matches may be displayed. Words that do not match perfectly are displayed with a question mark (“?”) suffixed.

The input data will only contain cases that fall within the preceding rules.

InputThe Morse code table will appear first and consists of lines each containing an uppercase letter or a digit C, zero or more blanks, and a sequence of no more than six periods and hyphens giving the Morse code for C. Blanks may precede or follow the items on the line. A line containing a single asterisk (“*”), possibly preceded or followed by blanks, terminates the Morse code table. You may assume that there will be Morse code given for every character that appears in thecontextsection.

Thecontextsection appears next, with one word per line, possibly preceded and followed by blanks. Each word incontextwill contain no more than ten characters. No characters other than upper case letters and digits will appear. Thered will be at most 100contextwords. A line containing only a single asterisk (“*”), possibly preceded or followed by blanks, terminates thecontextsection.

The remainder of the input contains morse words separated by blanks or end-of-line characters. A line containing only a single asterisk (“*”), possibly preceded or followed by blanks, terminates the input. Nomorseword will have more than eighty (80) elements.

OutputFor each inputmorseword, display the appropriate matching word fromcontextfollowed by an exclamation mark (“!”) or question mark (“?”) if appropriate. Each word is to appear on a separate line starting in column one.Sample InputA.-B-…C-.-.D-..E.F..-.G–.H….I..J.—K-.-L.-..M–N-.O—P.–.Q–.-R.-.S…T-U..-V…-W.–X-..-Y-.–Z–..0——1.—–2..—3…–4….-5…..6-….7–…8—..9—-.*ANEARTHQUAKEEATGODHATHIMREADYTOWHATWROTH*.–…..– …..–….–.—-.. .–.-.—-…–…..– .–…-.-.-….–.-..-.–.-…– .-…–..-.——..–*Sample OutputWHATHATHGODWROTH?WHATANEARTHQUAKEEAT!READYTOEAT!

,有一些喷着香水闻不到的空气,有一些在写字楼里永远遇不见的人。

508 Morse Mismatches

相关文章:

你感兴趣的文章:

标签云: