【紫书】(UVa12096) The SetStack Computer

时间:2023-03-09 18:42:51
【紫书】(UVa12096) The SetStack Computer

突然转进到第五章的low题目的原因是做到图论了(紫书),然后惊喜的发现第一题就做不出来。那么里面用到了这一题的思想,我们就先解决这题。当然,dp必须继续做下去,这是基本功。断不得。

题意分析

这条题真的是一条sb模拟题。但是我们还是可以学些什么:一,标id的思想(后面用到了,就是那条UVa12219)。二,set的基本操作。具体思路看代码。但是这题哪里有什么算法...set 和 map 混合应用,真的用板子写的话代码相近度很高

代码

#include <bits/stdc++.h>

#define NQUICKIO
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define REP(x) for(int i=0;i!=(x);++i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull; map<set<int>, int> m;
vector<set<int> > idxm;
inline int ID(set<int>& _s)
{
if(m.find(_s)!=m.end()) return m[_s];
idxm.push_back(_s);
return m[_s]=idxm.size()-1;
} int main()
{
#ifdef QUICKIO
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#endif
int kase; cin>>kase; while(kase--)
{
m.clear();
stack<int> s;
int n; cin>>n;
for(int i=1;i<=n;++i)
{
string tmpstr; cin>>tmpstr;
if(tmpstr[0]=='P')
{
set<int> tmp;
s.push(ID(tmp));
}
else if(tmpstr[0]=='D')
s.push(s.top());
else
{
auto s1=idxm[s.top()]; s.pop();
auto s2=idxm[s.top()]; s.pop();
set<int> tmps;
if(tmpstr[0]=='U')
set_union(ALL(s1),ALL(s2),INS(tmps));
if(tmpstr[0]=='I')
set_intersection(ALL(s1),ALL(s2),INS(tmps));
if(tmpstr[0]=='A')
{
tmps=s2; tmps.insert(ID(s1));
}
s.push(ID(tmps));
}
cout<<idxm[s.top()].size()<<endl;
}
cout<<"***"<<endl;
}
return 0;
}