当前位置:网站首页>The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
2022-06-28 20:36:00 【Love_ Jacques】
First Vjudge Address : Click here
subject :
There's one designed specifically for set operations “ Set stack ” Computer . The machine has a stack that starts empty and supports the following operations .
1.PUSH: An empty set “{}” Push .
2.DUP: Copy the current top of the stack element and put it on the stack .
3.UNION: Two sets out of the stack , Then put the union of the two on the stack .
4.INTERSECT: Two sets out of the stack , Then put the intersection of the two on the stack .
5.ADD: Two sets out of the stack , Then add the first out of stack set to the later out of stack set , Put the results on the stack .
for example , The top element of the stack is A = { {},{ {}} }, The next element is B = { {}, { { {}}} }, be :
UNION The operation will get { {}, { {}}, { { {}}} }, Output 3.
INTERSECT The operation will get { {} }, Output 1.
ADD The operation will get { {}, { { {}}},{ {} , { {}}} }, Output 3.
Input :
The first line has an integer T(0<=T<=5) Express T Group test data .
Each set of test data has N(0<=N<=2000) operations , Ensure smooth operation ( There is no need to perform an out of stack operation on an empty stack ).
Output :
For each operation specified in the input , Output the size of the stack top set ( That is, the number of elements ) And line breaks .
Output one line after each set of test data “***”.
Examples :
Intput
2
9
PUSH
DUP
ADD
PUSH
ADD
DUP
ADD
DUP
UNION
5
PUSH
PUSH
ADD
PUSH
INTERSECT
Output
0
0
1
0
1
1
2
2
2
***
0
0
1
0
0
***
Topic analysis ( According to the purple book ):
How to simulate the collection stack described in the topic ? The big guys are flying all over the sky stl The operation is really impressive ;
The point to be explained separately in this topic is , All sets in the topic are operated by an empty set , Therefore, the form of elements in a set is not limited to brackets or numbers , It can be extended more widely ;
data structure ( A top priority ):
Before explaining the choice of data structure , We know that every element in a set is unique , So you can give each element in the set Assign sequence number , Replace the element with a sequence number without operating on the element itself ;
for example :{ {}} Marked with serial number 1,{ {},{}} Marked with serial number 2,{ { {}},{}} Marked with serial number 3… wait ;
So we can use set<int> a Store each set ,
utilize map<set<int>,int> IDcache Store the correspondence between set and sequence number ;
utilize vector<set<int>> Setcache To get the contents of the collection through the sequence number ;
Definition stcak<int> s A stack that stores the sequence number of a collection ;
thus , We can already define the container and STL A series of operations required in the title are realized by the built-in function of ;
Algorithm design :
Just hard simulation ;
Module design : Definition and pretreatment – Read in and initialize – simulation – Output –return 0;
Code :
#include<bits/stdc++.h>
using namespace std;
typedef set<int> Set;
map<Set,int> IDcache;
vector<Set> Setcache;
int t,n;
int ID(Set x){
if(IDcache.count(x)) return IDcache[x]; // adopt map Get set label
Setcache.push_back(x);
return IDcache[x]=Setcache.size()-1; // If you can't find it, follow up the label
}
#define ALL(x) x.begin(),x.end() // Konjaku does not understand the macro , Just for a suit
#define INS(x) inserter(x,x.begin())
int main()
{
cin>>t;
while(t--)
{
stack<int> s;
cin>>n;
string order;
for(int i=0;i<n;i++)
{
cin>>order;
if(order=="PUSH") s.push(ID(Set()));
else if(order=="DUP") s.push(s.top());
else{
Set x1=Setcache[s.top()];
s.pop();
Set x2=Setcache[s.top()];
s.pop();
Set x;
if(order=="UNION") set_union (ALL(x1),ALL(x2),INS(x));
if(order=="INTERSECT") set_intersection (ALL(x1),ALL(x2),INS(x));
if(order=="ADD") {
x=x2; x.insert(ID(x1));}
s.push(ID(x));
}
cout<<Setcache[s.top()].size()<<endl;
}
cout<<"***"<<endl;
}
return 0;
}
Summary of key points and details :
- About... In the code
set_unionAndset_intersectionyes STL Built in functions for , Save in header file#include<set>,uinon Is a union ,intersection To get the intersection ; Use details reference here ; - Worship big brother STL Use ;
Update and 2020.7.3
边栏推荐
- 我也差点“跑路”
- Can layoffs really save China's Internet?
- Understand the construction of the entire network model
- 请问同业存单是否靠谱,安全吗
- 稳定性总结
- Jenkins pipeline's handling of job parameters
- How to use dataant to monitor Apache apisex
- Flatten of cnn-lstm
- Resilience4j retry source code analysis and retry index collection
- How to add logs to debug anr problems
猜你喜欢

rsync远程同步

Bitbucket 使用 SSH 拉取仓库失败的问题

2022 tea master (intermediate) examination simulated 100 questions and simulated examination

APISIX 助力中东社交软件,实现本地化部署

The principle and source code analysis of Lucene index construction
![[graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!](/img/e5/b6035abfa7d4bb59c3080d3b87ce45.jpg)
[graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!

with torch. no_ Grad(): reason for using

ref属性,props配置,mixin混入,插件,scoped样式

学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证

Win 10 create a gin framework project
随机推荐
Fix the simulator that cannot be selected by flutter once
2022 tea master (intermediate) examination simulated 100 questions and simulated examination
Ref attribute, props configuration, mixin mixing, plug-in, scoped style
LeetCode每日一题——710. 黑名单中的随机数
3. integrate listener
输入和输出实型数据
2022 P cylinder filling test exercises and online simulation test
算力时代怎么「算」?「算网融合」先发优势很重要!
不同框架的绘制神经网络结构可视化
【Try to Hack】Cobalt Strike(一)
题解 Pie(POJ3122)超详细易懂的二分入门
开通挖财账号安全吗?是靠谱的吗?
odoo15 Module operations are not possible at this time, please try again later or contact your syste
Can layoffs really save China's Internet?
Stability summary
Understand the construction of the entire network model
Day88. qiniu cloud: upload house source pictures and user avatars
大智慧上怎么进行开户啊, 安全吗
How to open an account in great wisdom? Is it safe
rapid ssl通配符证书八百一年是正版吗