Uva12096-The setstack computer (set + map ing)

Source: Internet
Author: User

Uva12096-The setstack computer (set + map ing)

Question Link

There are five actions:
Push: place an empty set {} to the top of the stack.
DUP: gets the set at the top of the stack and writes it to the stack twice.
Add: the first set is put into the second set as an element twice, and then the second set is put into the stack.
Union: returns the union of the two sets to the stack.
Intersect: the output stack is two times. The intersection of the two sets is used to import the result to the stack.
After each action is executed, the number of elements in the current stack top set needs to be output.

Solution: the stack and set can be used to simulate the problem. Push puts an empty set into the stack. However, when merging and intersection, You need to determine whether the segment set is the same, therefore, you can use map to manually map a collection that has occurred to a number.

Code:

#include <cstdio>#include <cstring>#include <stack>#include <set>#include <map>using namespace std;char op[15];int n;typedef set<int> E;stack<E> s;map<E, int> vis;E tmp1, tmp2;set<int>::iterator it;int num;void hash (E a) {    if (!vis.count(a))        vis[a] = ++num;}void Push () {    tmp1.clear();    s.push (tmp1);            }void Dup () {    tmp1 = s.top();    s.push (tmp1);}void Add () {    tmp2 = s.top();    s.pop();    tmp1 = s.top();    s.pop();    tmp1.insert (vis[tmp2]);    hash(tmp1);    s.push(tmp1);}void Union () {    tmp2 = s.top();        s.pop();    tmp1 = s.top();    s.pop();    for (it = tmp1.begin(); it != tmp1.end(); it++)        tmp2.insert (*it);    hash (tmp2);    s.push (tmp2);    }void Intersect () {    tmp2 = s.top();    s.pop();    tmp1 = s.top();    s.pop();    E tmp;    for (it = tmp1.begin(); it != tmp1.end(); it++)        if (tmp2.count(*it))            tmp.insert (*it);    hash (tmp);    s.push(tmp);}void solve () {    switch (op[0]) {        case ‘P‘ : Push();     break;        case ‘D‘ : Dup();      break;        case ‘U‘ : Union();    break;        case ‘I‘ : Intersect(); break;        case ‘A‘ : Add();      break;    }    printf ("%d\n", s.top().size()); }void init () {    num = 1;    while (!s.empty()) {        s.pop();    }    vis.clear();}int main () {    int T;    scanf ("%d", &T);    while (T--) {        scanf ("%d", &n);        while (n--) {            scanf ("%s", op);            solve();                        }        printf ("***\n");    }    return 0;}

Uva12096-The setstack computer (set + map ing)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.