UVA

来源:互联网 发布:js数组怎么添加元素 编辑:程序博客网 时间:2024/06/03 14:03

点击打开题目链接

Samuel F. B. Morse is best known for the coding scheme that carries his name. Morse code is still usedin international radio communication. The coding of text using Morse code is straightforward. Eachcharacter (case is insignificant) is translated to a predefined sequence of dits and dahs (the elementsof Morse code). Dits are represented as periods (‘.’) and dahs are represented as hyphens or minussigns (‘-’). Each element is transmitted by sending a signal for some period of time. A dit is rathershort, and a dah is, in perfectly formed code, three times as long as a dit. A short silent space appearsbetween elements, with a longer space between characters. A still longer space separates words. Thisdependence on the spacing and timing of elements means that Morse code operators sometimes do notsend perfect code. This results in difficulties for the receiving operator, but frequently the message canbe 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 themessage “dit dit dit” were received, it could be interpreted as “EEE”, “EI”, “IE” or “S” based on thecoding scheme shown in the sample input. To decide between these multiple interpretations, we assumea 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 Morsecode, a list of expected words (context), and a sequence of words encoded in Morse code (morse). Thesemorse words may be flawed. For each morse word, your program is to determine the matching wordfrom context, if any. If multiple words from context match morse, or if no word matches perfectly, yourprogram 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, byitself. If multiple context words match morse perfectly, then select the matching word with the fewestcharacters. If this still results in an ambiguous match, any of these matches may be displayed. Ifmultiple context words exist for a given morse, the first matching word will be displayed followed byan exclamation point (‘!’).We assume only a simple case of errors in transmission in which elements may be either truncatedfrom the end of a morse word or added to the end of a morse word. When no perfect matches formorse are found, display the word from context that matches the longest prefix of morse, or has thefewest extra elements beyond those in morse. If multiple words in context match using these rules, anyof these matches may be displayed. Words that do not match perfectly are displayed with a questionmark (‘?’) 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 adigit C, zero or more blanks, and a sequence of no more than six periods and hyphens giving the Morsecode 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 therewill 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 caseletters and digits will appear. Thered will be at most 100 context words. A line containing only a singleasterisk (‘*’), 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. Aline containing only a single asterisk (“*”), possibly preceded or followed by blanks, terminates theinput. 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 exclamationmark (‘!’) or question mark (‘?’) if appropriate. Each word is to appear on a separate linestarting 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!


题目大意:

给出每个字母的电码,给出一个单词字典,然后给出n个电码值,输出字典中相匹配的单词。多个单词匹配,按照字典顺序输出第一个并加’!‘,没有完全匹配的输出最佳匹配的加‘?’。

思路:

map存匹配字母,结构体存字典单词和匹配的电码,然后比较给出的电码即可。

因为‘?’的原因WA了一次

’?‘是在如果没有精确匹配,在电码尾部增加或删除尽量少的字符使其匹配。

附上AC代码:

#include<iostream>#include<map>#include<string>#include<cstring>#include<algorithm>using namespace std;const int maxn=100+5;const int INF=0x7f7f7f;map<char,string>_map;char c;string s,tmp;int cnt;struct dics{    string word,key;}dic[maxn];int cmp(string s1,string s2){    int num=0;    int flag=1;    if(s1.length()<s2.length()){        for(int i=0;i<s1.length();i++){            if(s1[i]!=s2[i]){                flag=0;                break;            }        }        if(flag==1){            num=s2.length()-s1.length();        }        else num=INF;    }    else if(s1.length()>s2.length()){        for(int i=0;i<s2.length();i++){            if(s1[i]!=s2[i]){                flag=0;                break;            }        }        if(flag==1){            num=s1.length()-s2.length();        }        else num=INF;    }    else num=INF;        return num;}int main(){    while(cin>>c&&c!='*'){        cin>>s;        _map[c]=s;    }    cnt=0;    while(cin>>s&&s[0]!='*'){        dic[cnt].word=s;        tmp="";        for(int i=0;i<s.length();i++){            tmp+=_map[s[i]];        }        dic[cnt++].key=tmp;    }    int flag=0,num=0;    string ans;    while(cin>>s&&s[0]!='*'){        flag=num=0;        for(int i=0;i<cnt;i++){            if(dic[i].key==s){                num++;                if(num==1)ans=dic[i].word;                flag=1;            }        }        if(flag==1){            if(num==1)cout<<ans<<endl;            else cout<<ans<<"!"<<endl;        }        else{            int _min=INF;            ans="";            for(int i=0;i<cnt;i++){                if(cmp(dic[i].key,s)<_min){                    _min=cmp(dic[i].key,s);                    ans=dic[i].word;                }            }            ans+='?';            cout<<ans<<endl;        }    }    return 0;}