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 of dits and dahs (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 each morse word, your program is to determine the matching word from context, if any. If multiple words from context match morse, or if no word matches perfectly, your program will display the best matching word and a mismatch indicator.

If a single word from context matches morse perfectly, it will be displayed on a single line, by itself. If multiple context words exist for a given morse, 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 a morse word or added to the end of a morse word. When no perfect matches for morse are found, display the word from context that matches the longest prefix of morse, or has the fewest extra elements beyond those in morse. If multiple words in context match 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.

Input

The 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 the context section.

The context section appears next, with one word per line, possibly preceded and followed by blanks. Each word in context will contain no more than ten characters. No characters other than upper case letters and digits will appear. Thered will be at most 100 context words. A line containing only a single asterisk (``*''), possibly preceded or followed by blanks, terminates the context 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 will have more than eighty (80) elements.

Output

For each input morse word, display the appropriate matching word from context followed by an exclamation mark (``!'') or question mark (``?'') if appropriate. Each word is 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 ----.
*
AN
EARTHQUAKE
EAT
GOD
HATH
IM
READY
TO
WHAT
WROTH
*
.--.....--
.....--....
--.----..
.--.-.----..
.--.....--
.--.
..-.-.-....--.-..-.--.-.
..--
.-...--..-.--
----
..--
*
Sample output
WHAT
HATH
GOD
WROTH?
WHAT
AN
EARTHQUAKE
EAT!
READY
TO
EAT! 给定一些已知字典,给定一些编码,求解这些编码的对应原文,如果可以精确匹配,则直接输出原单词,
如果有多个可精确匹配的结果,则输出匹配结果里字典序最小的单词并在末位加上“!”;
如果无法精确匹配,则可以在编码的末尾增加或删除一些字符后匹配单词(增删量最小),无论增删后匹配一个还是多个,最后都要加上“?”;
如果有多个模糊匹配结果(增删数相等),则输出字典序最小的匹配结果;
如果无精确匹配也无模糊匹配结果,则输出整个给定字典里字典序最小的那个单词。 看清题意就好,用map做,不难!
#include <iostream>
#include <map>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std; map <char, string>morse;
string words[],codes[];
int n;
map<char,string>::iterator t; string change(string word)
{
char code[];
int wordsize = word.length(),p = ; for(int i = ;i < wordsize; i++){ for(t = morse.begin();t != morse.end();t++){ if((*t).first == word[i]){
int morsesize = ((*t).second).length();
for(int j = ;j < morsesize;j++ )code[p++] = ((*t).second)[j];
break;
}
}
}
code[p] = '\0';
return code;
} void detail(string co)
{
int p = co.length(),a,b=,c;
bool flag;
for(int i = ;i < n;i++){
int q = codes[i].length();
flag = true;
if(q > p){
for(int j = ;j < p;j++){
if(co[j] != codes[i][j]){
flag = false;
break;
}
}
}
else{
for(int j = ;j < q;j++){
if(co[j] != codes[i][j]){
flag = false;
break;
}
}
}
if(flag){
a = abs(q - p);
c = i;
}
if(flag && a < b){
b = a;
c = i;
}
}
cout << words[c] << "?" <<endl;
} void deal(string co)
{
int m = ;
for(int i = ;i < n;i++){
if(co == codes[i]){
if(m == )cout << words[i];
m++;
}
}
if(m == )cout << endl;
else if(m > ) cout << "!" << endl;
else if(m == )detail(co);
} int main()
{
char ch; while(cin >> ch){ //单词对应的编码
if(ch == '*')break;
string mima;
cin >> mima;
morse[ch] = mima;
} for(int i = ; ; i++){ //字典中的单词
cin >> words[i];
if(words[i][] == '*'){
n = i;
break;
}
}
sort(words , words + n); //字典序从小到大排列
for(int i = ;i < n;i++){
//cout<<"-**-"<<words[i]<<endl;
codes[i] = change( words[i] );
} string co;
while(cin >> co){
if(co[] == '*')break;
deal(co);
} //system("pause");
return ;
}

提交以上代码....发现 还是结果错误

有两个小错误,通过以下测试得出的

1.

S ...
O ---
T -
*
SO
ST
*
...--

得到的结果是ST 而正确答案是SO     经检查发现detail函数中第一个flag只判断第一次true的情况,所以加上条件cot即可

2.

S ...
O ---
T -
V ...-
I ..
A .-
*
SO
IAT
*
...---.

得到的结果是IAT  而正确答案是SO       经检查发现detail函数中给a赋值不是只在第一次flag判断中赋值,所有情况为都要给a赋值,由于上一部加上cot条件导致这一步出错所以将a的赋值语句移至flag为true情况外即可

AC代码

 #include <iostream>
#include <map>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std; map <char, string>morse;
string words[],codes[];
int n;
map<char,string>::iterator t; string change(string word)
{
char code[];
int wordsize = word.length(),p = ; for(int i = ;i < wordsize; i++){ for(t = morse.begin();t != morse.end();t++){ if((*t).first == word[i]){
int morsesize = ((*t).second).length();
for(int j = ;j < morsesize;j++ )code[p++] = ((*t).second)[j];
break;
}
}
}
code[p] = '\0';
return code;
} void detail(string co)
{
int p = co.length(),a,b=,c,cot=; //q为待检测电码长度
bool flag;
for(int i = ;i < n;i++){
int q = codes[i].length(); //p为原单词电码的长度
//cout<<words[i]<<" "<<codes[i]<<" "<<q<<endl;
flag = true;
if(q > p){
for(int j = ;j < p;j++){
if(co[j] != codes[i][j]){
flag = false;
break;
}
}
}
else{
for(int j = ;j < q;j++){
if(co[j] != codes[i][j]){
flag = false;
break;
}
}
}
a = abs(q - p);
if(flag && cot){
c = i;
cot = ;
//cout<<"aa "<<a<<" "<<c<<endl;
}
if(flag && a < b){
b = a;
c = i;
}
}
cout << words[c] << "?" <<endl;
} void deal(string co)
{
int m = ;
for(int i = ;i < n;i++){
if(co == codes[i]){
if(m == )cout << words[i];
m++;
}
}
if(m == )cout << endl;
else if(m > ) cout << "!" << endl;
else if(m == )detail(co);
} int main()
{
char ch; while(cin >> ch){ //单词对应的编码
if(ch == '*')break;
string mima;
cin >> mima;
morse[ch] = mima;
} for(int i = ; ; i++){ //字典中的单词
cin >> words[i];
if(words[i][] == '*'){
n = i;
break;
}
}
sort(words , words + n); //字典序从小到大排列 for(int i = ;i < n;i++){
codes[i] = change( words[i] );
/*cout<<"words "<<words[i]<<endl;
cout<<"codes "<<codes[i]<<endl;*/
} string co;
while(cin >> co){
if(co[] == '*')break;
deal(co);
} //system("pause");
return ;
}

uva 508 Morse Mismatches的更多相关文章

  1. uva 508 - Morse Mismatches(摩斯码)

    来自https://blog.csdn.net/su_cicada/article/details/80084529 习题4-6 莫尔斯电码(Morse Mismatches, ACM/ICPC Wo ...

  2. UVA 508 Morse Mismatches JAVA

    题意:输入字母和数字的编码,输入词典,输入一段编码,求出对应的单词. 思路:来自https://blog.csdn.net/qq_41163933/article/details/82224703 i ...

  3. UVa 508 Morse Mismatches (模糊暴力)

    题意:莫尔斯电码,输入若干个字母的Morse编号,一个字典和若干编码.对于每个编号,判断它可能的是哪个单词, 如果有多个单词精确匹配,输出第一个单词并加一个“!”:如果无法精确匹配,那么在编码尾部增加 ...

  4. [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...

  5. 【习题 4-6 UVA - 508】Morse Mismatches

    [链接] 我是链接,点我呀:) [题意] 给你每个字母对应的摩斯密码. 然后每个单词的莫斯密码由其组成字母的莫斯密码连接而成. 现在给你若干个莫斯密码. 请问你每个莫斯密码对应哪个单词. 如果有多个单 ...

  6. UVA 567 Risk【floyd】

    题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=508">https://uva ...

  7. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  8. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

随机推荐

  1. Lightoj1009 Back to Underworld(带权并查集)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Back to Underworld Time Limit:4000MS      ...

  2. [总结] Stack: Java V.S. C++

    小结一下Stack 的主要API操作. 在c++ 和 java 中,stack 的操作几乎相同,只有查询栈顶元素一项操作的名称不同 (top() v.s. peek()) . 此外,在构造函数中,Ja ...

  3. Oracle11g R2学习系列 之二基本概念和环境介绍

    昨天安装好了之后,发现用Chrome打开OEM发现是英文的,搞得我好奇怪:安装时明明自动显示的是中文的,为何会是英文的呢.后来想想会不会是Oracle用的是浏览器的语言呢,果断打开Chrome的设置, ...

  4. PHP面向对象编程快速入门

    面向对象编程(OOP)是我们编程的一项基本技能,PHP4对OOP提供了良好的支持.如何使用OOP的思想来进行PHP的高级编程,对于提高PHP编程能力和规划好Web开发构架都是非常有意义的.下面我们就通 ...

  5. python的and与or剖析

    1.只含有and的表达式 In []: and True and ' Out[]: ' In []: and and True and 'long' Out[]: 从左向右,遇到False,则返回改值 ...

  6. 远程调试 Azure 上的 Website

    让我们先检查一下使用的 Azure SDK 版本和 Visual Studio 版本.根据MSDN的介绍,Azure 的远程调试功能是在 Azure SDK 2.2 中加入的,所以请确保您的机器上安装 ...

  7. 给div中动态添加节点并设置样式

    前端IOS今天需要动态的在图片前面添加一个按钮 主要是在使用 bt.setAttribute("class","aaa"); 可以对创建的节点使用setAttr ...

  8. Android 开发中关于layoutinflater

    Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout ...

  9. 使用git pull时,项目没有更新?

    进入项目目录后,执行 git pull 命令,没有将项目更新,并提示下图: 提示:there is no tracking information for the current branch. 意思 ...

  10. linux centos6.4 php连接sql server2008

    1.安装SQL Server驱动freetds yum search freetds yum install freetds php-mssql 或者下载编译安装   2.修改/etc/freetds ...