UVa 12096 The SetStack Computer 【STL】【stack】
生活随笔
收集整理的這篇文章主要介紹了
UVa 12096 The SetStack Computer 【STL】【stack】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接:點擊打開鏈接
紫書116頁
AC代碼:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> #include <set> #include <map> #include <vector> #include <stack> using namespace std; #define ALL(x) x.begin(),x.end() #define INS(x) inserter(x, x.begin()) typedef set<int> Set; map<Set,int> IDcache; vector<Set> Setcache;int ID(Set x) {if(IDcache.count(x)) return IDcache[x];Setcache.push_back(x);return IDcache[x] = Setcache.size() - 1; }int main() {int t, n;while(~scanf("%d",&t)){while(t--){stack<int> s;scanf("%d",&n);while(n--){string op;cin >> op;if(op[0] == 'P') s.push(ID(Set()));else if (op[0] == 'D') s.push(s.top());else{Set x1 = Setcache[s.top()]; s.pop();Set x2 = Setcache[s.top()]; s.pop();Set x;if(op[0] == 'U') set_union(ALL(x1), ALL(x2), INS(x));if(op[0] == 'I') set_intersection(ALL(x1), ALL(x2), INS(x));if(op[0] == 'A') { x = x2; x.insert(ID(x1)); }s.push(ID(x));}cout << Setcache[s.top()].size() << endl;}cout << "***" << endl;}}return 0; }幾個用法: s.empty()?????? //堆??諡檎? s.pop()?????????? //移除棧頂元素 s.push()???????? //在棧頂增加元素 s.size()????????? //返回棧中元素數(shù)目 s.top()??????????? //返回棧頂元素
STL內(nèi)置函數(shù)
set_union 求交集
set_intersection 求并集
總結(jié)
以上是生活随笔為你收集整理的UVa 12096 The SetStack Computer 【STL】【stack】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android获取UI控件的宽高
- 下一篇: 关于车上那几块屏幕的一点思考