集合栈计算机 uva 12096(蒟蒻的总结,set,map,vector,stack)
思路:
刘汝佳老师的,啊,说了很重要,但是本蒟弱还是没理解透理解了的部分写在注释上了,以后补充,
忘大佬指点
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<string>
#include <algorithm>
using namespace std;
typedef set<int> Set;//含若干个元素的集合
map<Set,int> IDcache;//IDcache[s]代表某个集合的ID,
vector<Set> Setcache;//对于某个集合Setcache[ID]这里面还是ID的集合
//带参数的宏定义,这个意思是 ALL(x) = x.begin(),x.end()
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
int ID(Set x)//这里返回的是集合x在Setcache的下标
{
//如果这个集合已经有ID了则直接return
if(IDcache[x]) return IDcache[x];
//否则,先添加新集合
Setcache.push_back(x);
return IDcache[x] = Setcache.size() - 1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
//栈里面存的是依次递增的数字
//代表某集合在Setcache里的下标
stack<int> s;
for(int i=0;i<n;i++)
{
string op;//操作operation
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;
//将x1和x2取并集,并且放在集合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)) ;}//将x1的D插入到x中
s.push(ID(x));
}
//for(int j=0;j<Setcache.size();j++)
//{
// cout<<Setcache[j].size()<<" ";
// }
// cout<<endl;
// cout<<"栈:";
//cout<<s.size()<<endl;
cout<<Setcache[s.top()].size()<<endl;
}
printf("***\n");
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话