• Uva




    为每个不同的集合分配一个唯一的ID,每个集合表示成所包含元素的ID集合,这样集合就可以用STL的set<int>来表示。栈是一个stack<int>。

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cctype>
    #include <cstring>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <stack>
    
    using namespace std;
    
    typedef set<int> Set;
    map<Set, int> IDcache; // 每个集合都对应一个ID
    vector<Set> Setcache; // 根据ID找到集合
    // 对于任意的集合s,IDcache[s]就是它的ID,而Setcache[IDcache[s]]就是s本身。
    
    int ID(Set x) {
    	// 如果找不到集合x的ID,则分配一个新ID
    	if (IDcache.count(x)) {
    		return IDcache[x];
    	}
    	Setcache.push_back(x); // 添加新集合
    	return IDcache[x] = Setcache.size() - 1;
    }
    
    #define ALL(x) x.begin(),x.end() // 所有的内容
    #define INS(x) inserter(x,x.begin()) // 插入迭代器
    
    int main()
    {
    	int T, n;
    	cin >> T;
    	while (T--) {
    		// 题目要求的栈
    		stack<int> s;
    		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') {
    					// STL内置有set_union
    					set_union(ALL(x1), ALL(x2), INS(x));
    				}
    				else if (op[0] == 'I') {
    					// STL内置有set_intersection
    					set_intersection(ALL(x1), ALL(x2), INS(x));
    				}
    				else if (op[0] == 'A') {
    					x = x2;
    					x.insert(ID(x1));
    				}
    				s.push(ID(x));
    			}
    			cout << Setcache[s.top()].size() << endl;
    		}
    		cout << "***
    ";
    	}
    
    	return 0;
    }




  • 相关阅读:
    研修班第四次课笔记
    形象革命——穿搭
    对管理者的几点要求
    全链路压测
    项目管理最忌的5件事,千万不要忽视!
    2018年计划小目标(9月)PMP
    NLP是什么
    (深度好文)重构CMDB,避免运维之耻
    《转》我们不得不面对的中年职场危机
    项目管理,让自己更从容
  • 原文地址:https://www.cnblogs.com/zhangyaoqi/p/4591595.html
Copyright © 2020-2023  润新知