ArvinShaffer

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

紫书上描述有问题,如果有多个精确匹配的, 要输出字典序最小的, 这里因为他给的序列就是按字典序排好的, 所以输出第一个精确匹配的。这道题,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] = '\0';
            break;
        }
        int temp = 0;
        for (int i = 0; i < strlen(words[n]); i++) {
            int flag;
            if (words[n][i] >= 'A'&&words[n][i] <= 'Z') {
                flag = words[n][i] - 'A';
                for (int j = 0; j < strlen(letter[flag]); j++) {
                    wordsMap[n][temp++] = letter[flag][j];
                }
            }
            else if (words[n][i] >= '0'&&words[n][i] <= '9') {
                flag = words[n][i] - '0';
                for (int j = 0; j < strlen(digit[flag]); j++) {
                    wordsMap[n][temp++] = digit[flag][j];
                }
            }
        }
        n++;
    }
}

void decode()
{
    char str[110];
    int res, matches, offset;
    int i, j;
    while (cin >> str) {
        res = 0; matches = 0; offset = 110;
        if (str[0] == '*')break;
        int off = 0;
        for (i = 0; i < n; i++) {
            for (j = 0; j < strlen(str), j < strlen(wordsMap[i]); j++) {
                if (str[j] != wordsMap[i][j])break;
            }
            //off = (strlen(str)-j) > (strlen(wordsMap[i])-j) ? (strlen(str) - j) : (strlen(wordsMap[i]) - j);
            if (strlen(str)==j&& strlen(wordsMap[i])==j) {
                /*if (matches) {
                    if (strcmp(wordsMap[res], wordsMap[i]) > 0) {
                        res = i;
                    }
                }
                else {
                    res = i;
                }*/
                if (!matches)res = i;
                matches++;
            }
            else {
                if (matches)continue;
                int min_len = min(strlen(str), strlen(wordsMap[i]));
                int max_len = max(strlen(str), strlen(wordsMap[i]));
                if (j==min_len&&max_len-min_len<offset) {
                    res = i;
                    offset = max_len - min_len;
                }
                /*else if (offset == off) {
                    if (strcmp(wordsMap[res], wordsMap[i]) > 0) {
                        res = i;
                    }
                }*/
            }
        }
        if (matches == 0) {
            cout << words[res] << '?' << endl;
        }
        else if (matches == 1) {
            cout << words[res] << endl;
        }
        else if (matches > 1) {
            cout << words[res] << '!' << endl;
        }
    }
}

int main()
{
    getMorseTable();
    getContext();
    decode();
    return 0;
}

 

posted on 2018-05-21 16:24  ArvinShaffer  阅读(200)  评论(0编辑  收藏  举报