紫书上描述有问题,如果有多个精确匹配的, 要输出字典序最小的, 这里因为他给的序列就是按字典序排好的, 所以输出第一个精确匹配的。这道题,wa了两次在算模糊匹配的时候,考虑不周全,代码注释部分就是,模糊匹配,要么增加编码,要么减少编码,不够的话就算匹配不上,第一次没考虑这点,所以wa了。
#include<iostream> #include<cstring> #include<algorithm> using namespace std; char digit[11][10], letter[27][10], words[105][30], wordsMap[105][90]; int n; void getMorseTable() { memset(digit, 0, sizeof(digit)); memset(letter, 0, sizeof(letter)); int i = 0; char str[30]; while (cin >> str) { if (str[0] == '*') { break; } if (str[0] >= 'A'&&str[0] <= 'Z') { i = str[0] - 'A'; cin >> letter[i]; } else if (str[0] >= '0'&&str[0] <= '9') { i = str[0] - '0'; cin >> digit[i]; } } } void getContext() { n = 0; memset(words, 0, sizeof(words)); memset(wordsMap, 0, sizeof(wordsMap)); while (cin >> words[n]) { if (words[n][0] == '*') { words[n][0] = '