UVA Online Judge solution 508 – Morse Mismatches – Solution in C++ – Volume 5
UVA Online Judge Solution 508 – Morse Mismatches | Volume 5
UVA Problem Link – https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=449
Problem Name: 508 – Morse Mismatches solution
Problem Number : UVA – 508 – Morse Mismatches solution
Online Judge : UVA Online Judge Solution
Volume: 5
Solution Language : C plus plus
UVA Solution 508 – Morse Mismatches Code in CPP:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
using namespace std;
map<string, string> morse;
map<string, string> dict;
int dist(string s1, string s2) {
if (s1 == s2) return 0;
if (s1.length() > s2.length())
swap(s1, s2);
if (s1 != s2.substr(0, s1.length())) return 0x3f3f3f3f;
return (int) (s2.length() - s1.length());
}
string encode(string s) {
string ret = "";
for (int i = 0; i < s.length(); i++)
ret += morse[string(1, s[i])];
return ret;
}
string decode(string s) {
string ret = dict.begin()->first;
int mn = 0x3f3f3f3f;
for (map<string, string>::iterator it = dict.begin();
it != dict.end(); it++) {
int d = dist(it->second, s);
if (d < mn) {
mn = d, ret = it->first;
} else if (d == mn && d == 0 && *ret.rbegin() != '!') {
ret += "!";
}
}
if (mn)
ret += "?";
return ret;
}
int main() {
string s1, s2;
while (cin >> s1 && s1 != "*") {
cin >> s2;
morse[s1] = s2;
}
while (cin >> s1 && s1 != "*")
dict[s1] = encode(s1);
while (cin >> s1 && s1 != "*")
cout << decode(s1) << endl;
return 0;
}
Tags: UVA Online Judge Solution, UVA OJ Solution list, UVA Problems Solution, UVA solver, UVA all problem solution list, UVA code in C, UVA code in C++, UVA solution in C, UVA solution, UVA OJ problems solution, UVA solution, UVA online judge codes, UVA problem 508 – Morse Mismatches solution, UVA Solution in C, UVA 508 – Morse Mismatches solution in C++, UVA 508 – Morse Mismatches solution in java