【例题5-5 UVA 12096 】The SetStack Computer

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

用set来解决这个问题。 考虑如何表示 { {{}} }这个集合 我们可以把{}这个集合和一个数字映射->1 然后把1加入到某个set里面去 即 {1} 则这就对应了->{ {} } 然后把{1}也用一个int对应->2 然后把2加入一个集合 {2} 则这个就对应了->{ {{}} } 这样,每个集合都能用set来表示了. 则用map < set ,int>来搞映射。 然后正常地用set求交集、并集就好了。 ->交集并集set都有现成的函数的。 栈里面可以只存这个set的映射就行。

【错的次数】

在这里输入错的次数

【反思】

这种一层套一层的方法好巧妙。。

【代码】

#include <bits/stdc++.h>
using namespace std;

typedef set<int> Set;

map <Set, int> mymap;
stack <int> sta;
vector <Set> what;

int ID(Set x)
{
	if (mymap.find(x) != mymap.end()) return mymap[x];
	what.push_back(x);
	mymap[x] = (int)what.size() - 1;
	return mymap[x];
}

int main()
{
	//freopen("F:\rush.txt", "r", stdin);
	ios::sync_with_stdio(0), cin.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		what.clear();
		while (!sta.empty()) sta.pop();
		mymap.clear();
		int n;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			string s;
			cin >> s;
			if (s[0] == 'P')
				sta.push(ID(Set()));
			else
				if (s[0] == 'D')
					sta.push(sta.top());
				else
				{
					Set x = what[sta.top()]; sta.pop();
					Set y = what[sta.top()]; sta.pop();
					Set temp;
					if (s[0] == 'U')
						set_union(x.begin(), x.end(), y.begin(), y.end(), inserter(temp, temp.begin()));
					if (s[0] == 'I')
						set_intersection(x.begin(), x.end(), y.begin(), y.end(), inserter(temp, temp.begin()));
					if (s[0] == 'A')
					{
						temp = y;
						temp.insert(ID(x));
					}
					sta.push(ID(temp));
				}
			cout << (int)what[sta.top()].size() << endl;
		}
		cout << "***" << endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7656600.html