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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

The SetStack Computer

發(fā)布時(shí)間:2024/4/30 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 The SetStack Computer 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原題及翻譯

Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century.
來自維基百科的背景:“集合論是一個(gè)數(shù)學(xué)分支,主要由19世紀(jì)末的德國數(shù)學(xué)家喬治坎特創(chuàng)立。
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 in mathematics concerning the existence of mathematical objects (such as numbers or functions) and their properties.
最初有爭議的是,集合論在現(xiàn)代數(shù)學(xué)中起到了基礎(chǔ)理論的作用,從某種意義上說,集合論被用來證明數(shù)學(xué)中關(guān)于數(shù)學(xué)對象(如數(shù)字或函數(shù))的存在及其性質(zhì)的假設(shè)是合理的。
Formal versions of set theory also have a foundational role to play as specifying a theoretical ideal of mathematical rigor in proofs.”
集理論的正式版本也有一個(gè)基礎(chǔ)性的作用,作為在證明中指定數(shù)學(xué)嚴(yán)謹(jǐn)?shù)睦碚摾硐搿!?br /> 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.
鑒于集合的重要性,作為數(shù)學(xué)的基礎(chǔ),一組古怪的理論家著手建造一臺以集合而不是數(shù)字為基礎(chǔ)的超級計(jì)算機(jī)。
The initial SetStack Alpha is under construction, and they need you to simulate it in order to verify the operation of the prototype.
初始的setstack alpha正在構(gòu)建中,他們需要您模擬它以驗(yàn)證原型的操作。
The computer operates on a single stack of sets, which is initially empty.
計(jì)算機(jī)在最初為空的一組集合上運(yùn)行。
After each operation, the cardinality of the topmost set on the stack is output.
在每次操作之后,將輸出堆棧上最頂層集的基數(shù)。
The cardinality of a set S is denoted |S| and is the number of elements in S.
集合s的基數(shù)用|s|表示,是s中元素的數(shù)目。
The instruction set of the SetStack Alpha is PUSH, DUP, UNION, INTERSECT,and ADD.
setstack alpha的指令集是push、dup、union、intersect和add。
? PUSH will push the empty set {} on the stack.
?push將推動堆棧上的空集合。
? DUP will duplicate the topmost set (pop the stack, and then push that set on the stack twice).
?DUP將復(fù)制最上面的集合(彈出堆棧,然后將該集合推到堆棧上兩次)。
? UNION will pop the stack twice and then push the union of the two sets on the stack.
?UNION將彈出堆棧兩次,然后推動堆棧上兩組的聯(lián)合。
? INTERSECT will pop the stack twice and then push the intersection of the two sets on the stack.
?Intersect將彈出堆棧兩次,然后推動堆棧上兩個(gè)集合的交叉點(diǎn)。
? ADD will pop the stack twice, add the first set to the second one, and then push the resulting set on the stack.
?ADD將彈出堆棧兩次,將第一組添加到第二組,然后將結(jié)果集推送到堆棧上。
For illustration purposes, assume that the topmost element of the stack is
A = {{}, {{}}}
and that the next one is
B = {{}, {{{}}}}
為了便于說明,假設(shè)堆棧的最上面的元素是
A = {{}, {{}}}
下一個(gè)是
B = {{}, {{{}}}}
For these sets, we have |A| = 2 and |B| = 2. Then:
對于這些集合,我們有a=2和b=2。然后:
? UNION would result in the set {{}, {{}}, {{{}}}}. The output is 3.
?UNION將導(dǎo)致集合{{}, {{}}, {{{}}}}。輸出為3。
? INTERSECT would result in the set {{}}. The output is 1.
?INTERSECT將導(dǎo)致集合 {{}}。輸出為1。
? ADD would result in the set {{}, {{{}}}, {{},{{}}}}. The output is 3.
?添加將導(dǎo)致集合 {{}, {{{}}}, {{},{{}}}}。輸出為3。

Input

An integer 0 ≤ T ≤ 5 on the first line gives the cardinality of the set of test cases.
第一行的整數(shù)0≤t≤5給出了一組測試用例的基數(shù)。
The first line of each test case contains the number of operations 0 ≤ N ≤ 2000.
每個(gè)測試用例的第一行包含操作數(shù)0≤n≤2000。
Then follow N lines each containing one of the five commands.
然后N行有五個(gè)命令。
It is guaranteed that the SetStack computer can execute all the commands in the sequence without ever popping an empty stack.
它保證了setstack計(jì)算機(jī)可以按順序執(zhí)行所有命令,而不會彈出空堆棧。

Output

For each operation specified in the input, there will be one line of output consisting of a single integer.
對于輸入中指定的每個(gè)操作,將有一行輸出,由單個(gè)整數(shù)組成。
This integer is the cardinality of the topmost element of the stack after the corresponding command has executed.
這個(gè)整數(shù)是執(zhí)行相應(yīng)命令后堆棧最頂層元素的基數(shù)。
After each test case there will be a line with ‘***’ (three asterisks).
每個(gè)測試用例之后都會有一行帶有“***”(三個(gè)星號)。

Sample Input

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

Sample Output

分析

本題的集合并不是簡單的整數(shù)集合或者字符集合,而是集合的集合。為了方便起見,此處為每個(gè)不同的集合分配一個(gè)唯一的ID,則每個(gè)集合都可以表示成所包含元素的ID的集合,這樣就可以用STL的set來表示了,而整個(gè)棧則是一個(gè)stack。

代碼

#include<iostream> #include<stack> #include<set> #include<map> #include<vector> #include<string> #include <algorithm>#define ALL(x) x.begin(),x.end() #define INS(x) inserter(x,x.begin()) /* 定義兩個(gè)宏,分別表示“所有的內(nèi)容”以及“插入迭代器”,具體作用可以從代碼中推斷出來, */using namespace std;typedef set<int> Set; map<Set,int> IDcache; //把集合映射成ID vector<Set> Setcache; //根據(jù)ID取集合//查找給定集合x的ID。如果找不到,分配一個(gè)新ID。 int ID(Set x) {if(IDcache.count(x)) return IDcache[x];Setcache.push_back(x); //添加新集合return IDcache[x]=Setcache.size()-1; }/* 對任意集合s(類型是上面定義的Set),IDcache[s]就是它的ID, 而Setcache[IDcache[s]]就是s本身。 *///主程序int main() {int T;cin >> T;while (T--) {stack<int> s;//題目中的棧int n;cin >> n;for (int i = 0; i < n; i++) {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; }

總結(jié)

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

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