Morse mismatches
Time Limit: 3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld &%llu |
Submit Status
Description
Samuel F. B Morse is best known for the coding scheme, the carries his name. Morse code is still used in international radio communication. The coding of the text using Morse code is straightforward. Each character (case was insignificant) is translated to a predefined sequence of dits and Dahs (The elements of Morse code). Dits is represented as periods (".") and dahs are represented as hyphens or minus signs ('-'). Each element was transmitted by sending a signal for some period of time. A dit is rather short, and a dah are, 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 in the spacing and timing of elements means that Morse code operators sometimes does not send perfect code. This results is difficulties for the receiving operator, but frequently the message can is decoded depending on context.
in the problem we consider reception of words in Morse code without spacing Betwee N Letters. Without the spacing, it's 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 ' bas Ed on the coding scheme shown in the sample input. To decide between these multiple interpretations, we assume a particular context by expecting all received word to appear In a dictionary.
for this problem your program would read a table giving the encoding of letters and Digits into Morse code, a list of the expected words ( context ), and a sequence of words encoded in Morse code ( mo RSE ). These Morse words may be flawed. For each Morse Word, your program was to determine the matching word from< span> context , if any. If multiple words from context match Morse , or if no word matches perfectly, your program would display the best matching word and a mismatch indicator.
If a single word from context Matches Morse perfectly, it'll be displayed on a single line, by itself.< SPAN>&NBSP If multiple context words exist for a given Morse , the first matching word would be is displayed followed by a exclamation point ('! ').
We assume only a simple case of errors in transmission in which elements could be either truncated from the end of a Morse Word or added to the end of a morse Word. When no perfect matches for Morse is found, display the word from context c11> that matches the longest prefix ofMorse, or have the fewest extra elements beyond those in Morse. If multiple words in context match using these rules, any of the these matches may be display Ed. Words that does not match perfectly is displayed with a question mark ('? ') suffixed.
The input data would only be contain cases that fall within the preceding rules.
InputThe Morse Code table would appear first and consists of lines each containing an uppercase letter or a digit C, zero or mor e 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. Assume that there would be Morse code given for every character that appears in the
Context Section.
The context section appears next, with one word per line, possibly preceded and followed by bla Nks. Each word in context would contain no more than ten characters. No characters other than upper case letters and digits would appear. thered'll is at the most of the context words. A line containing only a single asterisk ("*"), possibly preceded or followed by blanks, terminates the Co ntext section.
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. No morse Word'll has more than eighty elements.
OutputFor each input
Morse Word, display the appropriate matching word from
Context followed by an exclamation mark ('! ') or question mark ("?") if appropriate. Each word was to appear on a separate line starting in column one.Sample Input
A .-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 Output
Whathathgodwroth? whatanearthquakeeat! readytoeat!
for multiple exact matches, the output dictionary order is the smallest and "! ”。 Fuzzy matching, to output the most matching degree (it should be ...) No harm anyway) and "? ”。 With STL's words is very simple, the difficult place in the fuzzy matching processing, actually also fortunately. Write a comment and look at the code.
#include <iostream> #include <cstring> #include <string> #include <map> #include <iterator >using namespace Std;map<char, string>code; Code array with letters and their corresponding Morse code map<string, string>dict; Dict array of CDs and their corresponding code sequences int main () {char c;string cc;while (cin >> c&&c! = ' * ')//stored letter Morse code {CIN >> Cc;cod E[C] = cc;} /*map<char, String>::iterator it;for (it = Code.begin (); It! = Code.end (); it++) cout << It->first << "<< It->second << Endl; */string Word; Code sequence of a certificate of deposit while (CIN >> word && word!= "*") {string temp= "", for (int i = 0; word[i]; i++) temp = temp + Code[wo Rd[i]];d ict[word] = temp;} /*map<string, String>::iterator it;for (it = Dict.begin (); It! = Dict.end (); it++) cout << It->first << ; "" << it->second << endl;*/string s;while (Cin >> s && s! = "*") {int pipei = 0;map<string , how many words in the String>::iterator it;//statistical dictionary match This Code sequence for (it= Dict.begin (); It! = Dict.end (); it++) {if (It->second = = s) {if (!pipei) cout << it->first;pipei++;}} if (Pipei > 1) cout << '! '; if (Pipei = = 0)//For sequences that cannot be precisely matched special handling {string ans;string a;string b;int d;int dis=10e6; Dis record matching degree (difference of several letters) for (it = Dict.begin (); it = Dict.end (); it++) {a = It->second;b = S;if (B.size () < A.size ()) Swap (A, B); The SUBSTR function returns a copy of the length of a string starting with 0 if (a = = B.substr (0, A.size ()))//short sequence is a substring of the long sequence {d = b.size ()-a.size (); if (d < dis)//traversal find a hit The largest matching {dis = D;ans = It->first;}}} cout << ans << '? ';} cout << Endl;}}
UVA-508 Morse Mismatches