// 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;
}
华为OD机试-服务失效判断
最新推荐文章于 2023-10-07 00:05:47 发布