AcWing 5018. LDAP
原题链接
中等
作者:
Lumen3ever
,
2023-05-26 22:46:15
,
所有人可见
,
阅读 62
就是编译原理的东西 悔不当初啊
//这里填你的代码^^
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
unordered_map<int, vector<PII>> attr_u; // key -> vector<int, int> -> first : user second : val
int n, m;
vector<int> base_expr() {
int key, val;
char op;
vector<int> res;
cin >> key >> op >> val;
if(op == ':') {
for(auto &x: attr_u[key]) {
if(x.second == val) {
res.push_back(x.first);
}
}
} else {
for(auto &x : attr_u[key]) {
if(x.second != val) {
res.push_back(x.first);
}
}
}
return res;
}
vector<int> expr() {
char c;
cin >> c;
vector<int> res;
if(isdigit(c)) {
cin.unget();
res = base_expr();
} else {
char ch;
cin >> ch; assert(ch == '(');
auto a = expr();
cin >> ch; assert(ch == ')');
cin >> ch; assert(ch == '(');
auto b = expr();
cin >> ch; assert(ch == ')');
if(c == '&') {
set_intersection(a.begin(), a.end(), b.begin(), b.end(), inserter(res, res.begin()));
} else if(c == '|') {
set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(res, res.begin()));
}
}
return res;
}
void print(vector<int> res) {
for(auto &x: res) {
cout << x << ' ';
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++) {
int dn, cnt;
cin >> dn >> cnt;
for(int j = 0; j < cnt; j++) {
int key, val;
cin >> key >> val;
attr_u[key].push_back({dn, val});
}
}
auto cmp = [](const PII &a, const PII &b) {
return a.first < b.first;
};
for(auto &v : attr_u) {
sort(v.second.begin(), v.second.end(), cmp);
}
cin >> m;
while(m--) {
print(expr());
}
return 0;
}
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~