STL - K - The SetStack Computer UVA - 12096 交集并集
注意set的交集和并集的写法
#include<bits/stdc++.h> using namespace std; map< set<int>,int > Map; vector< set<int> > Setcache; stack<int> sta; int ID( set<int> x) { if(Map.count(x)) return Map[x]; else Setcache.push_back(x); return Map[x]=Setcache.size()-1; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin>>t; while(t--) { int n; cin>>n; for(int i=0;i<n;i++) { string s; cin>>s; if(s[0] == 'P') sta.push( ID( set<int>() ) ); else if(s[0]=='D') sta.push(sta.top()); else { set<int> x1=Setcache[ sta.top() ]; sta.pop(); set<int> x2=Setcache[sta.top()]; sta.pop(); set<int> x; if(s[0]=='U') set_union(x1.begin(),x1.end(),x2.begin(),x2.end(), inserter(x,x.begin())); if(s[0]=='I') set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin())); if(s[0]=='A') { x=x2; x.insert(ID(x1)); } sta.push(ID(x)); } cout<< Setcache[sta.top()].size()<<endl; } cout<<"***"<<endl; } return 0; }
usingnamespacestd; map< set<int>,int > Map; vector< set<int> > Setcache; stack<int> sta; int ID( set<int> x) { if(Map.count(x)) return Map[x]; else Setcache.push_back(x); return Map[x]=Setcache.size()-1; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin>>t; while(t--) { int n; cin>>n; for(int i=0;i<n;i++) { string s; cin>>s; if(s[0] == 'P') sta.push( ID( set<int>() ) ); elseif(s[0]=='D') sta.push(sta.top()); else { set<int> x1=Setcache[ sta.top() ]; sta.pop(); set<int> x2=Setcache[sta.top()]; sta.pop(); set<int> x; if(s[0]=='U') set_union(x1.begin(),x1.end(),x2.begin(),x2.end(), inserter(x,x.begin())); if(s[0]=='I') set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin())); if(s[0]=='A') { x=x2; x.insert(ID(x1)); } sta.push(ID(x)); } cout<< Setcache[sta.top()].size()<<endl; } cout<<"***"<<endl; } return0; }