uva508 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!

题意大概就是,给你每个字母的编码、可能出现的单词,让你从输入的一串字符中匹配单词。
如果能精确匹配,并且不会有歧义,就直接输出;
如果能精确匹配,但可能结果有多个,则输出字典序最靠前的那个,并在后面加个“!”;
如果不能精确匹配,则进行模糊匹配,就是增加或删除尽量少的字符,使之能匹配到一个给的单词,如果有多个,则输出字典序最靠前的那个,并加个“?”。

#include<bits/stdc++.h>
using namespace std;
char a[500][20];
//string b[110];
struct sss{
    string name;
    string mname;
}b[110];
bool cmp(sss a,sss b){
    return a.name<b.name;
}
int main()
{
    char c;
    while(cin>>c,c!='*'){
        cin>>a[c-'0'];
    }
    //cout<<a['O'-'0']<<endl;
    int n=0;string ch;
    for(int i=0;;i++){
        cin>>ch;
        if(ch[0]=='*') break;
        b[i].name=ch;
        for(int j=0;b[i].name[j]!='\0';j++){
            b[i].mname+=a[b[i].name[j]-'0'];//先把每个单词的编码都存起来
        }
        n++;//单词的个数
    }
    sort(b,b+n,cmp);//按字典序排序
    string s;
    for(int i=0;;i++){
        int m=0,flag=1000000;
        cin>>s;int len=s.size();//cout<<s<<endl;
        if(s[0]=='*') break;
        for(int j=0;j<n;j++){
            if(b[j].mname==s){
                m++;
                if(flag>j)
                    flag=j;
            }
        }
        if(m>1){
            cout<<b[flag].name<<"!\n";continue;
        }
        else if(m==1){
            cout<<b[flag].name<<endl;continue;
        }
        int diff=1000000,j;flag=0;
        for( j=0;j<n;j++){
            int fl=1;int llen=b[j].mname.size();
            for(int k=0;k<len&&k<llen;k++){
                if(b[j].mname[k]!=s[k]){
                    fl=0;break;
                }
            }

            if(!fl) continue;
            int l=abs(len-llen);
            if(l<diff){
                flag=j;diff=l;
            }
        }
        cout<<b[flag].name<<"?\n";
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值