题意:给一些对集合的操作,询问每一次操作后栈顶的集合元素个数
分析:首先{}是空的,每一次add时候,{} -> { {} }变成了有一个元素的集合,利用set和stack,map容器能很方便解决这道题。
if (!mp[s1]) mp[s1] = ++cnt; s2.insert (mp[s1]); } sta.push (s2); return s2.size (); } int intersect() { pop (); tmp.clear (); for (it=s1.begin (); it!=s1.end (); ++it) { if (s2.find (*it) != s2.end ()) { tmp.insert (*it); } } sta.push (tmp); return tmp.size (); } int _union() { pop (); for (it=s1.begin (); it!=s1.end (); ++it) { s2.insert (*it); } sta.push (s2); return s2.size (); } }; int main(void) { int T; scanf ("%d", &T); while (T--) { int n; scanf ("%d", &n); Opera oper; char str[10]; for (int i=0; i<n; ++i) { scanf ("%s", &str); if (str[0] == 'P') printf ("%d ", oper.push ()); else if (str[0] == 'D') printf ("%d ", oper.dup ()); else if (str[0] == 'A') printf ("%d ", oper.add ()); else if (str[0] == 'U') printf ("%d ", oper._union ()); else printf ("%d ", oper.intersect ()); } puts ("***"); } return 0; }