uva 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 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[103],codes[103];
int n;
map<char,string>::iterator t;

string change(string word)
{
    char code[100];
    int wordsize = word.length(),p = 0;

    for(int i = 0;i < wordsize; i++){

        for(t = morse.begin();t != morse.end();t++){

            if((*t).first == word[i]){
                int morsesize = ((*t).second).length();
                for(int j = 0;j < morsesize;j++ )code[p++] = ((*t).second)[j];
                break;
            }
        }
    }
    code[p] = '';
    return code;
}

void detail(string co)
{
    int p = co.length(),a,b=100,c;
    bool flag;
    for(int i = 0;i < n;i++){
        int q = codes[i].length();
        flag = true;
        if(q > p){
            for(int j = 0;j < p;j++){
                if(co[j] != codes[i][j]){
                    flag = false;
                    break;
                }
            }
        }
        else{
            for(int j = 0;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 = 0;
    for(int i = 0;i < n;i++){
        if(co == codes[i]){
            if(m == 0)cout << words[i];
            m++;
        }
    }
    if(m == 1)cout << endl;
    else if(m > 1) cout << "!" << endl;
    else if(m == 0)detail(co);
}

int main()
{
    char ch;

    while(cin >> ch){                   //单词对应的编码
        if(ch == '*')break;
        string mima;
        cin >> mima;
        morse[ch] = mima;
     }

     for(int i = 0; ; i++){                //字典中的单词
        cin >> words[i];
        if(words[i][0] == '*'){
            n = i;
            break;
        }
     }
    sort(words , words + n);           //字典序从小到大排列
    for(int i = 0;i < n;i++){
        //cout<<"-**-"<<words[i]<<endl;
        codes[i] = change( words[i] );
    }

     string co;
     while(cin >> co){
        if(co[0] == '*')break;
        deal(co);
     }

    //system("pause");
    return 0;
}

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

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

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代码

  1 #include <iostream>
  2 #include <map>
  3 #include <cmath>
  4 #include <string>
  5 #include <algorithm>
  6 using namespace std;
  7 
  8 map <char, string>morse;
  9 string words[103],codes[103];
 10 int n;
 11 map<char,string>::iterator t;
 12 
 13 string change(string word)
 14 {
 15     char code[100];
 16     int wordsize = word.length(),p = 0;
 17 
 18     for(int i = 0;i < wordsize; i++){
 19 
 20         for(t = morse.begin();t != morse.end();t++){
 21 
 22             if((*t).first == word[i]){
 23                 int morsesize = ((*t).second).length();
 24                 for(int j = 0;j < morsesize;j++ )code[p++] = ((*t).second)[j];
 25                 break;
 26             }
 27         }
 28     }
 29     code[p] = '';
 30     return code;
 31 }
 32 
 33 void detail(string co)
 34 {
 35     int p = co.length(),a,b=1000,c,cot=1;       //q为待检测电码长度
 36     bool flag;
 37     for(int i = 0;i < n;i++){
 38         int q = codes[i].length();       //p为原单词电码的长度
 39         //cout<<words[i]<<" "<<codes[i]<<" "<<q<<endl;
 40         flag = true;
 41         if(q > p){                             
 42             for(int j = 0;j < p;j++){
 43                 if(co[j] != codes[i][j]){
 44                     flag = false;
 45                     break;
 46                 }
 47             }
 48         }
 49         else{
 50             for(int j = 0;j < q;j++){
 51                 if(co[j] != codes[i][j]){
 52                     flag = false;
 53                     break;
 54                 }
 55             }
 56         }
 57         a = abs(q - p);
 58         if(flag && cot){
 59             c = i;
 60             cot = 0;
 61             //cout<<"aa "<<a<<" "<<c<<endl;
 62         }
 63         if(flag && a < b){
 64             b = a;
 65             c = i;
 66         }
 67     }
 68     cout << words[c] << "?" <<endl;
 69 }
 70 
 71 void deal(string co)
 72 {
 73     int m = 0;
 74     for(int i = 0;i < n;i++){
 75         if(co == codes[i]){
 76             if(m == 0)cout << words[i];
 77             m++;
 78         }
 79     }
 80     if(m == 1)cout << endl;
 81     else if(m > 1) cout << "!" << endl;
 82     else if(m == 0)detail(co);
 83 }
 84 
 85 int main()
 86 {
 87     char ch;
 88 
 89     while(cin >> ch){                   //单词对应的编码
 90         if(ch == '*')break;
 91         string mima;
 92         cin >> mima;
 93         morse[ch] = mima;
 94      }
 95 
 96      for(int i = 0; ; i++){                //字典中的单词
 97         cin >> words[i];
 98         if(words[i][0] == '*'){
 99             n = i;
100             break;
101         }
102      }
103     sort(words , words + n);           //字典序从小到大排列
104 
105     for(int i = 0;i < n;i++){
106         codes[i] = change( words[i] );
107         /*cout<<"words  "<<words[i]<<endl;
108         cout<<"codes  "<<codes[i]<<endl;*/
109     }
110 
111 
112      string co;
113      while(cin >> co){
114         if(co[0] == '*')break;
115         deal(co);
116      }
117 
118     //system("pause");
119     return 0;
120 }
原文地址:https://www.cnblogs.com/farewell-farewell/p/5412875.html