#include <bits/stdc++.h>
using namespace std;
int n = 0; //用户数目
int m = 0; //匹配表达式数目
typedef long long ll;
struct user {
int dn;
unordered_map<ll, ll>mp; //分别表示属性维度和属性值
} a[2510];
vector<int> match(string s) {
vector<int>ans;
if (s.find(":") != string::npos) {
int index = s.find(":");
int key = stoi(s.substr(0, index));
int value = stoi(s.substr(index + 1, s.length() - index - 1));
for (int i = 0; i < n; i++) {
if (a[i].mp.count(key))
if (a[i].mp[key] == value)
ans.push_back(a[i].dn);
}
} else if (s.find("~") != string::npos) {
int index = s.find("~");
int key = stoi(s.substr(0, index));
int value = stoi(s.substr(index + 1, s.length() - index - 1));
for (int i = 0; i < n; i++) {
if (a[i].mp.count(key))
if (a[i].mp[key] != value)
ans.push_back(a[i].dn);
}
}
sort(ans.begin(), ans.end());
return ans;
}
vector<int> match2(string s) {
if (s[0] > '0' && s[0] <= '9') {
return match(s);
} else {
char s0 = s[0];
s.erase(0, 1);
int index ;
for (int i = 1; i <= s.length(); i++) {
string temp = s.substr(0, i);
if (count(temp.begin(), temp.end(), '(') == count(temp.begin(), temp.end(), ')')) {
index = i;
break;
}
}
string s1 = s.substr(1, index - 2);
string s2 = s.substr(index + 1, s.length() - index - 2);
vector<int> vec1 = match2(s1);
vector<int> vec2 = match2(s2);
vector<int> ans;
if (s0 == '&') {
set_intersection(vec1.begin(), vec1.end(),
vec2.begin(), vec2.end(), back_inserter(ans));
return ans;
} else if (s0 == '|') {
set_union(vec1.begin(), vec1.end(),
vec2.begin(), vec2.end(), back_inserter(ans));
return ans;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int number = 0;
scanf("%d%d", &a[i].dn, &number);
while (number--) {
int character, value;
scanf("%lld%lld", &character, &value);
a[i].mp[character] = value;
}
}
scanf("%d", &m);
while (m--) {
string s;
cin >> s;
vector<int>ans = match2(s);
sort(ans.begin(), ans.end());
if (ans.size() == 0)
cout << endl;
else {
for (auto i : ans)
cout << i << " ";
cout << endl;
}
}
return 0;
}
CCF-CSP 202303-3 LDAP
最新推荐文章于 2024-08-31 15:34:19 发布