|
|
#include <bits/stdc++.h>
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
//保存字符--莫尔斯电码
|
|
|
map<char, string> char_morse_map;
|
|
|
|
|
|
//保存单词--莫尔斯电码
|
|
|
map<string, string> word_morse_map;
|
|
|
|
|
|
//获取字符和单词的莫尔斯电码映射
|
|
|
void input_MorseMap() {
|
|
|
int i;
|
|
|
char ch;
|
|
|
string Morse, Words;
|
|
|
while ((cin >> ch) && ch != '*') {
|
|
|
cin >> Morse;
|
|
|
char_morse_map.insert(pair<char, string>(ch, Morse));
|
|
|
}
|
|
|
while ((cin >> Words) && Words[0] != '*') {
|
|
|
Morse = "", i = 0;
|
|
|
while (Words.length() != i)
|
|
|
Morse += (*char_morse_map.find(Words[i++])).second;
|
|
|
word_morse_map.insert(pair<string, string>(Words, Morse));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//比较两个莫尔斯电码的长度,返回匹配不成功的长度差
|
|
|
int getComplen(string src, string obj) {
|
|
|
int i = 0;
|
|
|
for (i = 0; src[i] == obj[i]; i++);
|
|
|
return obj.length() - i; //这么写for循环判断是不是相等,是不是太调皮了~ 这个长度差是从目标串中减去相同长度获取到的
|
|
|
}
|
|
|
|
|
|
//进行莫尔斯电码匹配单词
|
|
|
void printMorseData() {
|
|
|
bool flag;
|
|
|
string MorseWord;
|
|
|
int min, len;
|
|
|
map<string, string>::iterator iter, ret;
|
|
|
while (cin >> MorseWord && MorseWord[0] != '*') {
|
|
|
flag = false;
|
|
|
//精确匹配
|
|
|
for (iter = word_morse_map.begin(); iter != word_morse_map.end(); iter++)
|
|
|
if (iter->second == MorseWord)
|
|
|
if (flag) cout << "!";
|
|
|
else cout << iter->first, flag = true;
|
|
|
//模糊匹配 由于摩斯密码只能截断,所以获取的摩斯密码只能比原来的数据短
|
|
|
if (!flag) {
|
|
|
min = 80;
|
|
|
for (iter = word_morse_map.begin(); iter != word_morse_map.end(); iter++)
|
|
|
if (iter->second.length() > MorseWord.length()) {
|
|
|
len = getComplen(MorseWord, iter->second);
|
|
|
if (len < min)
|
|
|
min = len, ret = iter;
|
|
|
}
|
|
|
cout << ret->first << "?";
|
|
|
}
|
|
|
cout << endl;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//主函数
|
|
|
int main() {
|
|
|
//读入数据文件
|
|
|
//FILE *fp = freopen("data6.in", "r", stdin);
|
|
|
//重载输出文件
|
|
|
//freopen("data6.out", "w", stdout);
|
|
|
//使得cin cout的效率同scanf和printf一样
|
|
|
ios::sync_with_stdio(false);
|
|
|
|
|
|
//获取映射数据
|
|
|
input_MorseMap();
|
|
|
|
|
|
//打印摩斯数据
|
|
|
printMorseData();
|
|
|
|
|
|
//重定向打开之后,不关闭的话就很痛苦了,关闭则万事大吉~
|
|
|
//freopen("CON", "r", stdin);
|
|
|
//freopen("CON", "w", stdout);
|
|
|
return 0;
|
|
|
}
|
|
|
|