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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【HDU - 1968】【UVA - 12096】The SetStack Computer (模拟,集合求交集并集操作,STL实现)

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 1968】【UVA - 12096】The SetStack Computer (模拟,集合求交集并集操作,STL实现) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Background from Wikipedia: 揝et theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational theory in modern mathematics, in the sense of a theory invoked to justify assumptions made inmathematics concerning the existence of mathematical objects (such as numbers or functions) and their properties. Formal versions of set theory also have a foundational role to play as specifying a theoretical ideal of mathematical rigor in proofs.?Given this importance of sets, being the basis of mathematics, a set of eccentric theorist set off to construct a supercomputer operating on sets instead of numbers. The initial Set-Stack Alpha is under construction, and they need you to simulate it in order to verify the operation of the prototype.?

?

The computer operates on a single stack of sets, which is initially empty. After each operation, the cardinality of the topmost set on the stack is output. The cardinality of a set S is denoted |S| and is the number of elements in S. The instruction set of the SetStack Alpha is PUSH, DUP, UNION, INTERSECT, and ADD.?
?PUSH will push the empty set {} on the stack.?
?DUP will duplicate the topmost set (pop the stack, and then push that set on the stack twice).?
?UNION will pop the stack twice and then push the union of the two sets on the stack.?
?INTERSECT will pop the stack twice and then push the intersection of the two sets on the stack.?
?ADD will pop the stack twice, add the first set to the second one, and then push the resulting set on the stack.?
For illustration purposes, assume that the topmost element of the stack is A = {{}, {{}}} and that the next one is B = {{}, {{{}}}}.?
For these sets, we have |A| = 2 and |B| = 2. Then:?
?UNION would result in the set { {}, {{}}, {{{}}} }. The output is 3.?
?INTERSECT would result in the set { {} }. The output is 1.?
?ADD would result in the set { {}, {{{}}}, {{},{{}}} }. The output is 3.?

Input

An integer 0 <= T <= 5 on the first line gives the cardinality of the set of test cases. The first line of each test case contains the number of operations 0 <= N <= 2 000. Then follow N lines each containing one of the five commands. It is guaranteed that the SetStack computer can execute all the commands in the sequence without ever popping an empty stack.

Output

For each operation specified in the input, there will be one line of output consisting of a single integer. This integer is the cardinality of the topmost element of the stack after the corresponding command has executed. After each test case there will be a line with *** (three asterisks).

Sample Input

2 9 PUSH DUP ADD PUSH ADD DUP ADD DUP UNION 5 PUSH PUSH ADD PUSH INTERSECT

Sample Output

0 0 1 0 1 1 2 2 2 *** 0 0 1 0 0 ***

題目大意:集合的運算
有5種操作:

PUSH:?空集“{}”入棧。

DUP:把當前棧頂元素復制一份后再入棧。

UNION:出棧兩個集合,然后把二者的并集入棧。

INTERSECT:出棧兩個集合,然后把二者的交集入棧

ADD:出棧兩個集合,然后把先出棧的加入到后出棧的集合中,把結果入棧。

每次操作后輸出棧頂集合的大小。

解題報告:

? ? ?是集合的集合,為了表示不同的集合,可用一個整型ID表示,比如說用1表示{},集合{{}}就表示成{1},再用2作為其ID。于是題目就成了編碼問題,對每個新生成的集合,我們判斷該集合是否出現過,若未出現過,就給他分配一個新的ID,出現過的用已有的ID表示。所有集合用map表示集合與ID的對應關系,用一個隊列存集合,以便根據ID去集合。

于競賽,這種題目還是不太可能碰到的、、于學習C++這門語言與掌握STL,這倒是個不錯的題目。

對于那個置空,我們可以用C++中類的概念解釋,寫成if(op[0] == 'P') sk.push(getid(kong)); 或者因為我們知道他一定是1,所以也可以直接sk.push( 1 );

AC代碼:(795ms)

#include<bits/stdc++.h>using namespace std; int top; map<set<int>,int> s_i; map<int,set<int> > i_s; int getid(set<int> s) {if(s_i.find(s) != s_i.end()) return s_i[s];s_i[s] = ++top;i_s[top] = s;return top; }int main() { // freopen("in.txt","r",stdin);int t,m;char op[10];set<int> kong,st1,st2,st;kong.clear();cin>>t;while(t--) {top = 0;s_i.clear();i_s.clear();stack<int> sk;scanf("%d",&m);while(m--) {scanf("%s",op);if(op[0] == 'P') sk.push(getid(kong)); else if(op[0] == 'D') sk.push(sk.top());else {st1 = i_s[sk.top()];sk.pop();st2 = i_s[sk.top()];sk.pop();st.clear();//這一句必須加上,不然就waif(op[0] == 'U') {set_union(st1.begin(),st1.end(),st2.begin(),st2.end(),inserter(st,st.begin()));}else if(op[0] == 'I') set_intersection(st1.begin(),st1.end(),st2.begin(),st2.end(),inserter(st,st.begin()));else {st2.insert(getid(st1));st = st2;}sk.push(getid(st));}printf("%d\n",i_s[sk.top()].size());}printf("***\n");}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【HDU - 1968】【UVA - 12096】The SetStack Computer (模拟,集合求交集并集操作,STL实现)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。