20240109
This commit is contained in:
115
1335.cpp
Normal file
115
1335.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
class ClassType {
|
||||
public:
|
||||
|
||||
enum ImportType {
|
||||
kPublicImport, kProtectedImport, kPrivateImport
|
||||
};
|
||||
enum StatusType {
|
||||
kPublic = 4, kProtected = 3, kPrivate = 2, kUnvisible = 1, kNone = 0
|
||||
};
|
||||
void Import(ClassType &src, ImportType import_type) {
|
||||
if (import_type == ImportType::kPublicImport) {
|
||||
pub_anc.push_back(&src);
|
||||
} else if (import_type == ImportType::kProtectedImport) {
|
||||
prot_anc.push_back(&src);
|
||||
} else {
|
||||
priv_anc.push_back(&src);
|
||||
}
|
||||
}
|
||||
|
||||
void AddElement(const std::string &name, StatusType status) {
|
||||
elements[name] = status;
|
||||
}
|
||||
|
||||
StatusType QueryElement(const std::string &name) {
|
||||
return DFSSearch(this, name);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
static StatusType DFSSearch(ClassType *rt, const std::string name) {
|
||||
StatusType res = StatusType::kNone;
|
||||
if (rt->elements.find(name) != rt->elements.end()) res = rt->elements[name];
|
||||
for (int i = 0; i < rt->pub_anc.size(); i++) {
|
||||
StatusType sub_res = DFSSearch(rt->pub_anc[i], name);
|
||||
if (sub_res == StatusType::kPrivate) sub_res = StatusType::kUnvisible;
|
||||
res = (sub_res > res) ? sub_res : res;
|
||||
}
|
||||
for (int i = 0; i < rt->prot_anc.size(); i++) {
|
||||
StatusType sub_res = DFSSearch(rt->prot_anc[i], name);
|
||||
if (sub_res == StatusType::kPrivate) sub_res = StatusType::kUnvisible;
|
||||
if ((sub_res == StatusType::kPublic) ||
|
||||
(sub_res == StatusType::kProtected)) sub_res = StatusType::kProtected;
|
||||
res = (sub_res > res) ? sub_res : res;
|
||||
}
|
||||
for (int i = 0; i < rt->priv_anc.size(); i++) {
|
||||
StatusType sub_res = DFSSearch(rt->priv_anc[i], name);
|
||||
if (sub_res == StatusType::kPrivate) sub_res = StatusType::kUnvisible;
|
||||
if ((sub_res == StatusType::kPublic) ||
|
||||
(sub_res == StatusType::kProtected)) sub_res = StatusType::kPrivate;
|
||||
res = (sub_res > res) ? sub_res : res;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, StatusType>elements;
|
||||
std::vector<ClassType *>pub_anc, prot_anc, priv_anc;
|
||||
};
|
||||
int main() {
|
||||
#ifdef local
|
||||
freopen("pro.in", "r", stdin);
|
||||
#endif // ifdef local
|
||||
int n;
|
||||
std::unordered_map<std::string, ClassType> class_registery;
|
||||
std::cin >> n;
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::string name;
|
||||
std::cin >> name;
|
||||
class_registery[name];
|
||||
int k0; std::cin >> k0;
|
||||
for (int j = 0; j < k0; j++) {
|
||||
std::string method, srcname;
|
||||
std::cin >> method >> srcname;
|
||||
if (method == "public") class_registery[name].Import(
|
||||
class_registery[srcname],
|
||||
ClassType::ImportType::kPublicImport);
|
||||
else if (method == "private") class_registery[name].Import(
|
||||
class_registery[srcname],
|
||||
ClassType::ImportType::kPrivateImport);
|
||||
else class_registery[name].Import(
|
||||
class_registery[srcname],
|
||||
ClassType::ImportType::kProtectedImport);
|
||||
}
|
||||
int k1; std::cin >> k1;
|
||||
for (int j = 0; j < k1; j++) {
|
||||
std::string mode, element_name;
|
||||
std::cin >> mode >> element_name;
|
||||
if (mode == "public") class_registery[name].AddElement(element_name,
|
||||
ClassType::StatusType::kPublic);
|
||||
else if (mode == "private") class_registery[name].AddElement(element_name,
|
||||
ClassType::StatusType::kPrivate);
|
||||
else class_registery[name].AddElement(element_name,
|
||||
ClassType::StatusType::kProtected);
|
||||
}
|
||||
}
|
||||
int m;
|
||||
std::cin >> m;
|
||||
for (int i = 0; i < m; i++) {
|
||||
std::string class_name, element_name;
|
||||
std::cin >> class_name >> element_name;
|
||||
ClassType::StatusType stat = class_registery[class_name].QueryElement(
|
||||
element_name);
|
||||
switch (stat) {
|
||||
case ClassType::StatusType::kNone: std::cout << "None\n"; break;
|
||||
case ClassType::StatusType::kPrivate: std::cout << "Private\n"; break;
|
||||
case ClassType::StatusType::kProtected: std::cout << "Protected\n"; break;
|
||||
case ClassType::StatusType::kPublic: std::cout << "Public\n"; break;
|
||||
case ClassType::StatusType::kUnvisible: std::cout << "Can not Fetch\n"; break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user