网易雷火9.13笔试题最后一题
不得不说这次的笔试题太恶心了,第四题题目读了十分钟,写代码两小时,还是太菜了
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <unordered_map>
using namespace std;
vector<string> parseCommand(char* str) {
vector<string> ret;
string str_buf = "";
for (size_t i = 0; i < 200 && str[i] != '\0'; i++) {
if (str[i] == ' ') {
ret.push_back(str_buf);
str_buf = "";
}
else {
str_buf += str[i];
}
}
if (str_buf.size() != 0) {
ret.push_back(str_buf);
}
return ret;
}
class RedNode {
public:
RedNode() {}
RedNode(string name, int status) : node_name(name), node_status(status) {}
void setVisible() {
this->node_status = 1;
}
void setInvisible() {
this->node_status = 0;
}
bool getStatus() {
return this->node_status == 1 ? true : false;
}
string getName() {
return this->node_name;
}
private:
string node_name;
int node_status;
};
class RedNodeSet {
public:
RedNodeSet() {}
RedNodeSet(string name) : node_set_name(name), node_set_number(0) {}
void insert(RedNode* node) {
this->red_node_list.insert(node);
this->node_set_number++;
}
bool getStatus() {
bool ret = false;
for (auto iter = this->red_node_list.begin(); iter != this->red_node_list.end(); iter++) {
ret |= (*iter)->getStatus();
}
return ret;
}
string getName() {
return this->node_set_name;
}
private:
string node_set_name;
int node_set_number;
set<RedNode*> red_node_list;
};
class Button {
public:
Button() {}
Button(string name) : button_name(name), bind_node_number(0), bind_set_number(0) {}
void insert(RedNode* red_node) {
this->red_node_list.insert(red_node);
this->bind_node_number++;
}
void insert(RedNodeSet* red_set) {
this->red_set_list.insert(red_set);
this->bind_set_number++;
}
bool getStatus() {
bool ret = false;
for (auto iter = this->red_node_list.begin(); iter != this->red_node_list.end(); iter++) {
ret |= (*iter)->getStatus();
}
for (auto iter = this->red_set_list.begin(); iter != this->red_set_list.end(); iter++) {
ret |= (*iter)->getStatus();
}
return ret;
}
void removeNode(RedNode* node) {
this->red_node_list.erase(node);
this->bind_node_number--;
}
void removeSet(RedNodeSet* set) {
this->red_set_list.erase(set);
this->bind_set_number--;
}
void insertNode(RedNode* node) {
this->red_node_list.insert(node);
this->bind_node_number++;
}
void insertSet(RedNodeSet* set) {
this->red_set_list.insert(set);
this->bind_node_number++;
}
set<RedNode*>& getRedNodeList() {
return this->red_node_list;
}
set<RedNodeSet*>& getRedSetList() {
return this->red_set_list;
}
private:
string button_name;
int bind_node_number;
int bind_set_number;
set<RedNode*> red_node_list;
set<RedNodeSet*> red_set_list;
};
class Panel {
public:
Panel() {}
Panel(string name) : panel_name(name), bind_button_number(0) {}
void insert(Button* button) {
this->button_list.insert(button);
this->bind_button_number++;
}
bool getStatus() {
bool ret = false;
for (auto iter = this->button_list.begin(); iter != this->button_list.end(); iter++) {
ret |= (*iter)->getStatus();
}
return ret;
}
string getName() {
return this->panel_name;
}
private:
string panel_name;
int bind_button_number;
set<Button*> button_list;
};
int main() {
int n, m, x, y;
cin >> n >> m >> x >> y;
unordered_map<string, RedNode*> red_node_map;
unordered_map<string, RedNodeSet*> red_set_map;
unordered_map<string, Button*> button_map;
unordered_map<string, Panel*> panel_map;
// add redNode
for (int i = 0; i < n; i++) {
string name;
int status;
cin >> name >> status;
RedNode* new_node = new RedNode(name, status);
red_node_map[name] = new_node;
}
// add redNodeSet
for (int i = 0; i < m; i++) {
string name;
int cnt;
cin >> name >> cnt;
RedNodeSet* new_node_set = new RedNodeSet(name);
red_set_map[name] = new_node_set;
for (int j = 0; j < cnt; j++) {
string red_node_name;
cin >> red_node_name;
if (red_node_map.find(red_node_name) != red_node_map.end()) {
new_node_set->insert(red_node_map[red_node_name]);
}
}
}
// add Button
for (int i = 0; i < x; i++) {
string name;
int cnt;
cin >> name >> cnt;
Button* new_button = new Button(name);
button_map[name] = new_button;
for (int j = 0; j < cnt; j++) {
string red_node_name;
cin >> red_node_name;
// search in the red_node_map
if (red_node_map.find(red_node_name) != red_node_map.end()) {
new_button->insert(red_node_map[red_node_name]);
}
// search in the red_set_map
else if (red_set_map.find(red_node_name) != red_set_map.end()) {
new_button->insert(red_set_map[red_node_name]);
}
}
}
// add Panel
for (int i = 0; i < y; i++) {
string name;
int cnt;
cin >> name >> cnt;
Panel* new_panel = new Panel(name);
panel_map[name] = new_panel;
for (int j = 0; j < cnt; j++) {
string button_name;
cin >> button_name;
if (button_map.find(button_name) != button_map.end()) {
new_panel->insert(button_map[button_name]);
}
}
}
int s;
cin >> s;
char buf[200];
cin.getline(buf, 200);
while (s--) {
cin.getline(buf, 200);
vector<string> command = parseCommand(buf);
if (command[0] == "Show" && command.size() == 2) {
string red_node_name = command[1];
if (red_node_map.find(red_node_name) != red_node_map.end()) {
red_node_map[red_node_name]->setVisible();
}
}
else if (command[0] == "Hide" && command.size() == 2) {
string red_node_name = command[1];
if (red_node_map.find(red_node_name) != red_node_map.end()) {
red_node_map[red_node_name]->setInvisible();
}
}
else if (command[0] == "Ask" && command.size() == 2) {
string tmp_name = command[1];
// search in the button map
if (button_map.find(tmp_name) != button_map.end()) {
if (button_map[tmp_name]->getStatus()) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
// searhc in the panel map
else if (panel_map.find(tmp_name) != panel_map.end()) {
if (panel_map[tmp_name]->getStatus()) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
}
else if (command[0] == "Remove" && command.size() == 3) {
string button_name = command[1];
string node_name = command[2];
if (button_map.find(button_name) != button_map.end()) {
Button* button = button_map[button_name];
for (auto iter = button->getRedNodeList().begin(); iter != button->getRedNodeList().end(); iter++) {
if ((*iter)->getName() == node_name) {
button->removeNode(*iter);
break;
}
}
for (auto iter = button->getRedSetList().begin(); iter != button->getRedSetList().end(); iter++) {
if ((*iter)->getName() == node_name) {
button->removeSet(*iter);
break;
}
}
}
for (auto iter = panel_map.begin(); iter != panel_map.end(); iter++) {
Panel* cur_panel = iter->second;
if (cur_panel->getStatus()) {
cout << cur_panel->getName() << endl;
}
}
}
else if (command[0] == "Bind" && command.size() == 3) {
string button_name = command[1];
string node_name = command[2];
if (button_map.find(button_name) != button_map.end()) {
Button* button = button_map[button_name];
if (red_node_map.find(node_name) != red_node_map.end()) {
button->insert(red_node_map[node_name]);
}
else if (red_set_map.find(node_name) != red_set_map.end()) {
button->insert(red_set_map[node_name]);
}
}
for (auto iter = panel_map.begin(); iter != panel_map.end(); iter++) {
Panel* cur_panel = iter->second;
if (cur_panel->getStatus()) {
cout << cur_panel->getName() << endl;
}
}
}
}
return 0;
}
