Ultraviolet A 12096-The setstack computer
Question Link
A few operations: Push is to add an empty set to the top of the stack, DUP is to copy the set at the top of the stack, and union is to take the first two and put them back, int Is the first two to take the intersection back, add is to take the first two, the first as a set to add the second, each operation outputs the number of the top set of the stack
Idea: Use set and stack to simulate, and then use map to hash a set.
Code:
# 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 ;}