这道题主要考的STL一些用法吧。。。。做不出来就是对STL的一些函数不认识
主要学习了
set_union(c1.begin(),c1.end(),c2.begin(),c2.end(),inserter(c,c.begin());
set_intersection(c1.begin(),c1.end(),c2.begin(),c2.end(),inserter(c,c.begin());
这个2个STL函数用来取 并 交 集合
#include<cstdio> #include<cstring> #include<iostream> #include<set> #include<list> #include<algorithm> #include<map> #include<vector> #include<stack> #include<iostream> using namespace std; typedef set<int> Set; #define INS(x) inserter(x,x.begin()) #define ALL(x) x.begin(),x.end() map<Set,int>ID_array; vector<Set>Set_array; int Find_ID(Set x){ if(ID_array.find(x) != ID_array.end()) return ID_array[x]; else { Set_array.push_back(x); return ID_array[x] = Set_array.size() - 1; } } int main(){ int T; scanf("%d",&T); while(T--){ stack<int>st; int n; scanf("%d",&n); for(int i = 0 ; i < n ; i ++){ string s; cin >> s; if(s == "PUSH") st.push(Find_ID(set<int>())); else if(s == "DUP") st.push(st.top()); else { Set x1 = Set_array[st.top()]; st.pop(); Set x2 = Set_array[st.top()]; st.pop(); Set x; if(s == "UNION") set_union(ALL(x1),ALL(x2),INS(x)); else if(s == "INTERSECT") set_intersection(ALL(x1),ALL(x2),INS(x)); else if(s == "ADD"){ x = x2; x.insert(Find_ID(x1)); } st.push(Find_ID(x)); } printf("%d\n",Set_array[st.top()].size()); } printf("***\n"); } return 0; }