Created
January 22, 2020 16:26
-
-
Save WindAzure/d0bdd61d36bb3197b465fe5e47368334 to your computer and use it in GitHub Desktop.
UVa 12096
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <iterator> | |
#include <map> | |
#include <set> | |
#include <stack> | |
#include <string> | |
using namespace std; | |
int totalID = 1; | |
stack<int> Stack; | |
map<int, set<int>> idMappingTable; | |
map<set<int>, int> contentsMappingTable; | |
int popTopElement() | |
{ | |
auto topElement = Stack.top(); | |
Stack.pop(); | |
return topElement; | |
} | |
void pushToStackWith(set<int> &contents) | |
{ | |
if (contentsMappingTable.count(contents)) | |
{ | |
Stack.push(contentsMappingTable[contents]); | |
} | |
else | |
{ | |
Stack.push(totalID); | |
contentsMappingTable[contents] = totalID; | |
idMappingTable[totalID++] = contents; | |
} | |
} | |
void init() | |
{ | |
totalID = 1; | |
Stack = stack<int> {}; | |
idMappingTable.clear(); | |
contentsMappingTable.clear(); | |
idMappingTable[0] = set<int> {}; | |
contentsMappingTable[set<int> {}] = 0; | |
} | |
void addOperation(int firstItemID, int secondItemID) | |
{ | |
auto secondItemContents = idMappingTable[secondItemID]; | |
secondItemContents.insert(firstItemID); | |
pushToStackWith(secondItemContents); | |
} | |
void unionOperation(int firstItemID, int secondItemID) | |
{ | |
auto unionSet = set<int> {}; | |
set_union(idMappingTable[firstItemID].begin(), idMappingTable[firstItemID].end(), idMappingTable[secondItemID].begin(), | |
idMappingTable[secondItemID].end(), inserter(unionSet, unionSet.begin())); | |
pushToStackWith(unionSet); | |
} | |
void intersectOperation(int firstItemID, int secondItemID) | |
{ | |
auto intersectSet = set<int> {}; | |
set_intersection(idMappingTable[firstItemID].begin(), idMappingTable[firstItemID].end(), idMappingTable[secondItemID].begin(), | |
idMappingTable[secondItemID].end(), inserter(intersectSet, intersectSet.begin())); | |
pushToStackWith(intersectSet); | |
} | |
int main() | |
{ | |
auto T = 0, N = 0; | |
scanf("%d", &T); | |
for (auto i = 0; i < T; i++) | |
{ | |
init(); | |
scanf("%d", &N); | |
for (auto j = 0; j < N; j++) | |
{ | |
auto operationText = string {}; | |
cin >> operationText; | |
if (operationText == "PUSH") | |
{ | |
Stack.push(0); | |
} | |
else if (operationText == "DUP") | |
{ | |
Stack.push(Stack.top()); | |
} | |
else | |
{ | |
auto firstItemID = popTopElement(); | |
auto secondItemID = popTopElement(); | |
if (operationText == "ADD") | |
{ | |
addOperation(firstItemID, secondItemID); | |
} | |
else if (operationText == "UNION") | |
{ | |
unionOperation(firstItemID, secondItemID); | |
} | |
else if (operationText == "INTERSECT") | |
{ | |
intersectOperation(firstItemID, secondItemID); | |
} | |
} | |
printf("%d\n", idMappingTable[Stack.top()].size()); | |
} | |
printf("***\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment