华为OD机试-服务失效判断

// Online C++ compiler to run C++ program online
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<algorithm>
#include<iomanip>
#include<string.h>
#include<exception> 
#include<map>
#include<cmath>
#include<unordered_map>
#include<set>
#include<climits>
#include<ctype.h>
#include<queue>
#include<stack>
using namespace std;

struct Serv {
    string name;
    int id;
};

bool comp(Serv a, Serv b)
{
    if (a.id < b.id)
        return true;
    return false;
}

vector<string> split(string str, char ch) {
    vector<string> list;
    str += ch;
    while (str.find(ch) != string::npos)
    {
        int idx = str.find(ch);
        string token = str.substr(0, idx);
        str = str.substr(idx + 1);
        list.push_back(token);
    }

    return list;
}

int main() {
    string str;
    cin >> str;

    int id = 0;
    map<string, int> idmap;
    map<string, set<string>> depends;

    auto depList = split(str, ',');
    for (auto x : depList)
    {
        auto s = split(x, '-');

        if (depends.find(s[0]) == depends.end()) {
            idmap[s[0]] = id++;
            depends[s[0]] = {};
        }

        if (depends.find(s[1]) != depends.end()) {
            depends[s[1]].insert(s[0]);
        }
        else {
            idmap[s[1]] = id++;
            depends[s[1]] = { s[0] };
        }
    }
 
    cin >> str;
    auto failList = split(str, ',');

    for (auto x : failList) {
        if (depends.find(x) == depends.end())
            continue;
        for (auto z : depends[x]) {
            depends.erase(z);
        }
        depends.erase(x);
    }

    vector<Serv> list;
    for (auto x : depends)
    {
        list.push_back({ x.first, idmap[x.first] });
    }

    sort(list.begin(), list.end(), comp);

    for (int i = 0; i < list.size(); ++i) {
        cout << list[i].name;

        if (i < (list.size() - 1)) {
            cout << ',';
        }
    }
 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值