赞
踩
题目非常长,但还没有到完全看不懂的地步,即给你一些匹配表达式,让你输出对应的用户DN即可
该题,我就想奔着40分去的,因为40分只是符合EASY_EXPR即简单的匹配表达式,它只包含一个逻辑符号和两个括号内的简单式子如
&(1:2)(2:3)
数据结构上我选择vector与pair进行存储如下
因为如果只是满足EASY_EXPR,那么可能的情况会比较少,因此完全可以使用if将所有情况列出来,具体实现如下(不知为何0分)
#include<iostream> #include<vector> #include<string> #include<set> using namespace std; int n, m; typedef struct Node { int attr, value; }Node; vector<pair<int, vector<Node> > >arr; vector<int> fun1(string exp) {//得到:符号匹配用户的DN int key = exp[0] - 48, v = exp[2] - 48; vector<int>res;A for (int i = 0; i < n; i++) { pair<int, vector<Node> >per = arr[i]; int curDN = per.first; vector<Node>temp = per.second; int len = temp.size(); for (int j = 0; j < len; j++) { if (temp[j].attr == key && temp[j].value == v) { res.push_back(curDN); break; } } } return res; } vector<int> fun2(string exp) {//得到~符号匹配用户的DN int key = exp[0] - 48, v = exp[2] - 48; vector<int>res; for (int i = 0; i < n; i++) { pair<int, vector<Node> >per = arr[i]; int curDN = per.first; vector<Node>temp = per.second; int len = temp.size(); for (int j = 0; j < len; j++) { if (temp[j].attr == key && temp[j].value != v) { res.push_back(curDN); break; } } } return res; } int main() { cin >> n; vector<Node>temp; for (int i = 1; i <= n; i++) { int DN, count_attr, attr, value; temp.clear(); cin >> DN >> count_attr; for (int j = 1; j <= count_attr; j++) { cin >> attr >> value; temp.push_back({ attr,value }); } arr.push_back(make_pair(DN, temp)); } cin >> m; while (m--) { vector<int>ans; ans.clear(); string exp; cin >> exp; if (exp[0] == '&') { string sub1 = exp.substr(2, 3); string sub2 = exp.substr(7, 3); vector<int>res1, res2; set<int>st; if (sub1[1] == ':') { res1 = fun1(sub1); } else if (sub1[1] == '~') { res1 = fun2(sub1); } if (sub2[1] == ':') { res2 = fun1(sub2); } else if (sub2[1] == '~') { res2 = fun2(sub2); } //找到res1与res2共同拥有的部分 int len1 = res1.size(),len2=res2.size(); for (int i = 0; i < len1; i++) { st.insert(res1[i]); } for (int j = 0; j < len2; j++) { if (st.count(res2[j])) { ans.push_back(res2[j]); } } } else if (exp[0] == '|') { string sub1 = exp.substr(2, 3); string sub2 = exp.substr(7, 3); vector<int>res1, res2; set<int>st; if (sub1[1] == ':') { res1 = fun1(sub1); } else if (sub1[1] == '~') { res1 = fun2(sub1); } if (sub2[1] == ':') { res2 = fun1(sub2); } else if (sub2[1] == '~') { res2 = fun2(sub2); } //找到res1与res2拥有的部分去重加入到ans中 int len1 = res1.size(), len2 = res2.size(); for (int i = 0; i < len1; i++) { st.insert(res1[i]); } for (int j = 0; j < len2; j++) { st.insert(res2[j]); } for (auto i = st.begin(); i != st.end(); i++) { ans.push_back(*i); } } else if(exp.length()==3){ if (exp[1] == ':') { ans = fun1(exp); } else if (exp[1] == '~') { ans = fun2(exp); } } int len = ans.size(); for (int i = 0; i < len; i++) { if (i == 0) { cout << ans[i]; } else { cout << " " << ans[i]; } } cout << endl; } return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。