妈呀。。。clj大爷太强啦!

原来还有set_union和set_intersection这种东西。。。

于是只要把栈顶的每个元素hash一下记录到一个vector里去就好了

 

 1 /**************************************************************
 2     Problem: 1932
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:148 ms
 7     Memory:3372 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <vector>
12 #include <stack>
13 #include <algorithm>
14  
15 using namespace std;
16 typedef unsigned long long ull;
17 typedef vector <ull> vec;
18 const int N = 2e3 + 5;
19 const int base = 2333;
20  
21 inline ull add(const vec &v) {
22     static ull res;
23     static int i;
24     for (i = res = 0; i < v.size(); ++i)
25         res *= base, res += v[i] + 101;
26     return res;
27 }
28  
29 inline void get(vec &v) {
30     sort(v.begin(), v.end());
31     v.resize(unique(v.begin(), v.end()) - v.begin());
32 }
33  
34 int n, top;
35 vec S[N];
36  
37 int main() {
38     int i;
39     char st[15];
40     vec a, b, c(N);
41     scanf("%d",&n);
42     for (i = 1; i <= n; ++i) {
43         scanf("%s", st + 1);
44         if (st[1] == 'P') {
45             S[++top] = vec();
46             goto end;
47         }
48         if (st[1] == 'D') {
49             ++top, S[top] = S[top - 1];
50             goto end;
51         }
52         a = S[top--], b = S[top--];
53         if (st[1] == 'A') {
54             b.push_back(add(a)), get(b);
55             S[++top] = b;
56             goto end;
57         }
58         c = vec(a.size() + b.size());
59         if (st[1] == 'U') {
60             c.resize(set_union(a.begin(), a.end(), b.begin(), b.end(), c.begin()) - c.begin());
61             get(c), S[++top] = c;
62             goto end;
63         }
64         if (st[1] == 'I') {
65             c.resize(set_intersection(a.begin(), a.end(), b.begin(), b.end(), c.begin()) - c.begin());
66             get(c), S[++top] = c;
67             goto end;
68         }
69         end : printf("%d\n", S[top].size());
70     }
71     return 0;
72 }
View Code

 

posted on 2015-05-01 21:17  Xs酱~  阅读(343)  评论(0编辑  收藏  举报