-
这道题思路上来讲不太难,主要就是去模拟题目中所说的工作,需要注意的是查询的时候复杂度尽量压缩才能保证时间不会超时,所以本人代码开了哈希表和O2优化进行时间的压缩
-
当时做的时候一直超时没有想明白为什么,后来发现是在对
set
中的元素进行遍历的时候对这些元素又进行了进一步的合理性检查,但是其实这个检查我在将用户insert
进set
的时候已经进行过了,所以这时候进行检查会增加许多时间复杂度,导致TLE
-
改进代码也可以加入一些使用
bitset
压位的优化操作来减少空间复杂度
#include <bits/stdc++.h>
#define endl '\n'
#pragma GCC optimize(2)
typedef long long ll;
using namespace std;
const int N = 5e2 + 5;
int n, m, q;
struct actor {
unordered_map<string, bool> op;
unordered_map<string, bool> s_kind;
unordered_map<string, bool> s_name;
unordered_map<string, bool> group;
unordered_map<string, bool> user;
};
unordered_map<string, int> actors;
vector<actor> act;
unordered_map<string, vector<string>> toName;
void solve() {
cin >> n >> m >> q;
act.resize(n);
for (int i = 0; i < n; ++i) {
int nv, no, nn;
actor tmp;
string name;
cin >> name >> nv;
for (int j = 0; j < nv; ++j) {
string x; cin >> x;
tmp.op[x] = true;
}
cin >> no;
for (int j = 0; j < no; ++j) {
string x; cin >> x;
tmp.s_kind[x] = true;
}
cin >> nn;
for (int j = 0; j < nn; ++j) {
string x; cin >> x;
tmp.s_name[x] = true;
}
act[i] = tmp;
actors[name] = i;
}
for (int i = 0; i < m; ++i) {
string name; cin >> name;
int ns; cin >> ns;
for (int j = 0; j < ns; ++j) {
char f;
string u; cin >> f >> u;
toName[u].push_back(name);
if (f == 'g') act[actors[name]].group[u] = true;
if (f == 'u') act[actors[name]].user[u] = true;
}
}
for (int i = 0; i < q; ++i) {
bool res = false;
string usr; cin >> usr;
int ng; cin >> ng;
vector<string> gp(N);
set<string> tmpName;
for (int j = 0; j < ng; ++j) {
cin >> gp[j];
if (toName.find(gp[j]) != toName.end()) {
for (auto x : toName[gp[j]]) {
tmpName.insert(x);
}
}
}
if (toName.find(usr) != toName.end()) {
for (auto x : toName[usr]) {
tmpName.insert(x);
}
}
string a, b, c; cin >> a >> b >> c;
for (auto x : tmpName) {
bool f1 = false, f2 = false, f3 = false;
if ((act[actors[x]].op.find(a) != act[actors[x]].op.end()) ||
(act[actors[x]].op.find("*") != act[actors[x]].op.end())) f1 = true;
if ((act[actors[x]].s_kind.find(b) != act[actors[x]].s_kind.end()) ||
(act[actors[x]].s_kind.find("*") != act[actors[x]].s_kind.end())) f2 = true;
if ((act[actors[x]].s_name.empty()) || (act[actors[x]].s_name.find(c)
!= act[actors[x]].s_name.end())) f3 = true;
if (f1 && f2 && f3) {
res = true;
break;
}
}
if (res) cout << 1 << endl;
else cout << 0 << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
solve();
return 0;
}