UVa 508 Morse mismatches (vague violence)

Source: Internet
Author: User

Test instructions: Morse code, enter a number of letters of the Morse number, a dictionary and several encodings. For each number, determine which word it might be,

If more than one word matches exactly, output the first word and add a "! "; If you can't match exactly, add or remove as few characters as possible at the end of the code,

Make it match a word and add "? ”。

Analysis: The first time to do, a look at what ah, do not, now come back to see, found can be done, you can use the map of the STL to do, first of all to make a map of each letter,

Then make a map of the dictionary, the last input violence, each to look for, the increase of characters can think, that is, in comparison with the dictionary, short in the long string,

Intercept and short the same length of the string equal, then as long as the longer than a short length of the line, and finally take a shortest is OK. As for the plus "! "and"? "It's very simple.

I was wrong at first, and everything else was right. "Do not appear, must be optimistic about the problem."

The code is as follows:

#include <iostream> #include <cstdio> #include <cstring> #include <bits/stdc++.h>using namespace Std;const int INF = 0x3f3f3f;map<char, string> morse;map<string, string> dic;int judge (string A, str ing b) {if (a = = b) return 0;//equal if (A.size () > B.size ()) swap (A, b);//If the length of a is less than b if (a = = B.substr (0, A.size ()) return b.size ()-a.size ();//If the same as the front of a and B, then you can add or subtract characters to make them, return the length difference return inf;//If it is not the same, it cannot be added or removed to make them the same, then return the maximum value}st    Ring Solve (const string &s) {string ans = ""; int mmin = inf;//Initialize for (map<string, String>:: Iterator it = Dic.begin (); It! = Dic.end (); ++it) {int d =        Judge (S, It->second); if (!d &&!mmin && *ans.rbegin ()! = '! ')   {ans + = "!";  return ans; }//description Exact match twice, return directly OK if else if (d <= mmin) ans = it->first;//inexact match, mmin = min (d, mmin);/if (s  == ".--.-.----..")    cout << mmin << endl << it->second << Endl; } if (Mmin) aNS + = "?"; /If it is not exact match return ans;}    int main () {//Freopen ("In.txt", "R", stdin);    string s, ch;        while (cin >> ch && ch! = "*") {cin >> S;            Morse[ch[0]] = s;//constructs the Character Map} while (Cin >> s && s! = "*") {for (int i = 0; i < s.size (); ++i)    Dic[s] + = morse[s[i]];//Construction dictionary}//cout << dic["Wroth"] << Endl;    while (Cin >> s && s! = "*") cout << Solve (s) << Endl; return 0;}

UVa 508 Morse mismatches (vague violence)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.