uva 12096 - The SetStack Computer(STL)

UVA 12096 - The SetStack Computer

题目链接

题意:几个操作,push是在栈顶加入一个空集,dup是复制栈顶集合,在放入栈顶,union是把头两个取并集放回,int是头两个取交集放回,add是取头两个,把第一个当成一个集合加入第二个,每次操作输出栈顶集合的里面的个数

思路:用set,stack模拟,然后利用map去hash一个集合,模拟即可

代码:

#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

int t, n, hn;
char op[10];
stack<set<int> > st;
map<set<int>, int> hash;
set<int> s1, s2;
set<int>::iterator it;

void print() {
    printf("%d\n", (int)st.top().size());
}

void ad(set<int> s) {
    if (hash.count(s))
	return;
    hash[s] = hn++;
}

void pus() {
    set<int> s;
    ad(s);
    st.push(s);
    print();
}

void dup() {
    set<int> s = st.top();
    st.push(s);
    print();
}

void uni() {
    s1 = st.top(); st.pop();
    s2 = st.top(); st.pop();
    for (it = s1.begin(); it != s1.end(); it++)
	s2.insert(*it);
    ad(s2);
    st.push(s2);
    print();
}

void ins() {
    s1 = st.top(); st.pop();
    s2 = st.top(); st.pop();
    set<int> s3;
    it = s1.begin();
    set<int>::iterator it2 = s2.begin();
    while (it != s1.end() && it2 != s2.end()) {
	if (*it < *it2) *it++;
	else if (*it > *it2) it2++;
	else {
	    s3.insert(*it);
	    it++;
	    it2++;
	}
    }
    ad(s3);
    st.push(s3);
    print();
}

void add() {
    s1 = st.top(); st.pop();
    s2 = st.top(); st.pop();
    s2.insert(hash[s1]);
    ad(s2);
    st.push(s2);
    print();
}

int main() {
    scanf("%d", &t);
    while (t--) {
	hn = 0;
	hash.clear();
	while (!st.empty()) st.pop();
	scanf("%d", &n);
	while (n--) {
	    scanf("%s", op);
	    if (op[0] == 'P')
		pus();
	    if (op[0] == 'D')
		dup();
	    if (op[0] == 'A')
		add();
	    if (op[0] == 'U')
		uni();
	    if (op[0] == 'I')
		ins();
	}
	printf("***\n");
    }
    return 0;
}

uva 12096 - The SetStack Computer(STL),布布扣,bubuko.com

时间: 2024-10-25 07:16:58

uva 12096 - The SetStack Computer(STL)的相关文章

UVa - 12096 The SetStack Computer(STL容器综合,强推!)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42064 #include <iostream> #include <algorithm> #include <string> #include <map> #include <set> #include <vector> #include <stack> #define ALL(x) x.b

UVA - 12096 The SetStack Computer(编码,STL)

12096 The SetStack Computer Background from Wikipedia: "Set theory is a branch of mathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century.Initially controversial, set theory has come to play the role o

UVa 12096 The SetStack Computer

Description Background from Wikipedia: "Set theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational

uva 12096 - The SetStack Computer(STL 的运用)

这道题目貌似就是在不停地用STL中的内容,对STL熟练运用的大神估计坐起来会比较easy.. 不过对于我这种看着代码还是需要上网查函数运用的菜鸟来说,若让我自己做这道题,肯定不会使用STL.. 就当对STL的学习了. #include<cstdio> #include<iostream> #include<cstring> #include<string> #include<set> #include<stack> #include&

uva 12096 The SetStack Computer(STL set)

题意: 有5种操作: PUSH:加入"{}"空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出栈的加入到后出栈的集合中. 输入不超过2000, 保证操作顺利进行. 分析: 用set<int>(本身又可以映射成一个int)去模拟集合,所以可以将不同的集合映射成int型. 用一个Map<set<int>,int> 去映射成不同的int. 以后需要se

UVAOJ 12096 The SetStack Computer(STL的运用)

12096 The SetStack Computer Background from Wikipedia: Set theory is a branch ofmathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play therole of

12096 - The SetStack Computer UVA

Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational theory in m

12096 - The SetStack Computer

The SetStack Computer PS:因为该题排版较麻烦,这里给出OJ网址:UVa12096 - The SetStack Computer 有一个专门为了集合运算而设计的"集合栈"计算机.该机器有一个初始为空的栈,并且支持以下操作. PUSH:空集"{}"入栈. DUP:把当前栈顶元素复制一份后再入栈. UNION:出栈两个集合,然后把二者的并集入栈. INTERSECT:出栈两个集合,然后把二者的交集入栈. ADD:出栈两个集合,然后把先出栈的集合加

UVA 12096 STL map set 的使用

set这个容器也是STL库的一员,并且在algorithm内直接有 set_union set_intersection  这样求并集交集的操作 map 最方便的地方就是 支持下标访问 举例说明 : 1 #include<iostream> 2 include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<map> 6 #include<set> 7 #includ