题解 | #配置文件恢复#

配置文件恢复

http://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

哈希 map & set

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <utility>
#include <set>

using namespace std;

void matchCMD(const string &s); // 匹配给定命令
bool matchStr(const string &str1, const string &str2);    // 匹配两个字符串

int main() {
    string cmd;
    vector<string> cmds;
    while (getline(cin, cmd)) {
        cmds.push_back(cmd);
    }
    for (auto c : cmds) {
        matchCMD(c);
    }
    
    return 0;
}

void matchCMD(const string &s) {
    multimap<string, string> c***p = {    // 含两个字符串的命令的多重映射
        {"reset", "board"},
        {"board", "add"},
        {"board", "delete"},
        {"reboot", "backplane"},
        {"backplane", "abort"}
    };
    set<string> cmdSet = {"reset"};    // 含单个字符串的命令的集合
    map<pair<string, string>, string> executeMap1 = {    // 含两个字符串的命令所对应的执行内容
        {{"reset", "board"}, "board fault"},
        {{"board", "add"}, "where to add"},
        {{"board", "delete"}, "no board at all"},
        {{"reboot", "backplane"}, "impossible"},
        {{"backplane", "abort"}, "install first"}
    };
    map<string, string> executeMap2 = {    // 含单个字符串的命令所对应的执行内容
        {"reset", "reset what"}
    };
    // 标识给定命令是否有空格,从而判定该命令是双字符串命令还是单字符串命令
    bool sign = false;
    int spaceIndex = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == ' ') {
            
            sign = true;
            spaceIndex = i;
        }
    }
    
    if (sign) {
        string s1 = s.substr(0, spaceIndex - 1 - 0 + 1);    // 第一个字符串
        string s2 = s.substr(spaceIndex + 1, s.size() - 1- spaceIndex - 1 + 1);    // 第二个字符串
        int found = 0;    // 匹配到的命令的数量
        pair<string, string> sp;    // 匹配到的命令
        for (auto m : c***p) {    // 进行匹配
            if (matchStr(s1, m.first) && matchStr(s2, m.second)) {
                found++;
                sp.first = m.first;
                sp.second = m.second;
            }
        }
        if (found != 1) {    // 若没有匹配到或者匹配到的命令数量大于1
            cout << "unknown command" << endl;
        }
        else {    // 若只匹配到一条命令
            cout << executeMap1[{sp.first, sp.second}] << endl;
        }
    }
    else {    // 若该命令只含有一个字符串
        bool found = false;
        for (auto m : cmdSet) {
            if (matchStr(s, m)) {    // 若匹配到命令
                found = true;
                cout << executeMap2[m] << endl;
            }
        }
        if (!found) {    // 若没有匹配到命令
            cout << "unknown command" << endl;
        }
    }
}

bool matchStr(const string &str1, const string &str2) {
    auto it1 = str1.cbegin();
    auto it2 = str2.cbegin();
    while (it1 != str1.cend()) {
        if (it2 == str2.cend() || *it1 != *it2) {
            return false;
        }
        ++it1;
        ++it2;
    }
    return true;
}
全部评论

相关推荐

05-29 20:34
门头沟学院 C++
KarlAllen:得做好直接春招的准备。学历差的话,一是面试要求会比学历好的严格不少,二是就算面试通过了也会被排序。总之暑期和秋招对于学历差的就是及其不友好
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务