日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【ICPC-369】uva 12096 The SetStack Computer

發(fā)布時間:2024/3/13 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【ICPC-369】uva 12096 The SetStack Computer 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

點擊打開鏈接uva 12096

思路: STL模擬
分析:
1 題目給定5種操作,每次輸出棧頂集合的元素的個數(shù)
2 利用stack和set來模擬,set保存集合的元素。遇到push的時候直接在stack里面push入一個空的set,遇到Dup的時候把棧頂?shù)募显趐ush進stack一次,遇到union的時候把棧頂?shù)膬蓚€集合合并,遇到Intersect的時候把棧頂?shù)膬蓚€集合進行求交集然后push進stack,遇到Add的時候要注意如果第一個集合是空集那么我們就認為是在第二個集合里面加入2,否則就要通過map來判斷當前的集合所表示的值

代碼:

?

#include<set> #include<map> #include<stack> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std;const int N = 20; const int MAXN = 2010;int cnt; stack<set<int> >stk; map<set<int> , int>mp; set<int>s1 , s2;void pop(){s1 = stk.top();stk.pop();s2 = stk.top();stk.pop(); } // push void Push(){set<int>s;stk.push(s);puts("0"); } // dup void Dup(){set<int>s;s = stk.top();stk.push(s);printf("%d\n" , s.size()); } // union void Union(){pop();// set<int>::iterator it;for(it = s1.begin() ; it != s1.end() ; it++)s2.insert(*it);stk.push(s2);printf("%d\n" , s2.size()); } // Intersect void Intersect(){pop();//set<int>s3;set<int>::iterator it;for(it = s1.begin() ; it != s1.end() ; it++){if(s2.find(*it) != s2.end()) s3.insert(*it); }stk.push(s3);printf("%d\n" , s3.size()); } // add void Add(){pop();//if(s1.empty())s2.insert(0); else{if(!mp[s1])mp[s1] = cnt++;s2.insert(cnt++);}stk.push(s2);printf("%d\n" , s2.size()); }int main(){int Case , n;char str[N];scanf("%d" , &Case);while(Case--){scanf("%d" , &n); while(!stk.empty())stk.pop();cnt = MAXN;mp.clear();while(n--){scanf("%s" , str); if(str[0] == 'P')Push();else if(str[0] == 'D')Dup();else if(str[0] == 'U')Union();else if(str[0] == 'I')Intersect();elseAdd();} puts("***");}return 0; }

?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的【ICPC-369】uva 12096 The SetStack Computer的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。