1、题目大意:就是给你一个栈,有一些操作,向栈加入空集,把栈顶的元素复制一遍再加入栈,求栈顶两元素的并集,交集
还有栈的第一个元素和栈顶(将栈顶压缩成一个元素)
2、分析:这个其实不是用hash做的,是用系统地一堆函数做的。。
我用hash做的,其实就是暴力的hash,用ULL,用了一堆的STL。。。。不要去重,然后RKhash的长度记得取模
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <vector> #include <stack> using namespace std; #define ULL unsigned long long #define ygorz pair<ULL, int> stack<vector<ygorz> > st; char str[20]; ygorz qaq[200000]; ULL power[200000]; int main() { int n; scanf("%d", &n); power[0] = 1; for (int i = 1; i <= 199900; i++) power[i] = power[i - 1] * 107; for (int i = 1; i <= n; i++) { scanf("%s", str); if (str[0] == 'P') { vector<ygorz> a; a.clear(); st.push(a); } else if (str[0] == 'D') { st.push(st.top()); } else if (str[0] == 'U') { vector<ygorz> a = st.top(); st.pop(); vector<ygorz> b = st.top(); st.pop(); int u = 0, v = 0; int wt = 0; while (u < a.size() || v < b.size()) { if (u == a.size()) { qaq[++wt] = b[v]; v++; } else if (v == b.size()) { qaq[++wt] = a[u]; u++; } else { if (a[u] < b[v]) { qaq[++wt] = a[u]; u++; } else if (a[u] > b[v]) { qaq[++wt] = b[v]; v++; } else { qaq[++wt] = a[u]; u++; v++; } } } vector<ygorz> c; if(wt){ c.clear(); for (int i = 1; i <= wt; i++){ c.push_back(qaq[i]); } } st.push(c); } else if (str[0] == 'I') { vector<ygorz> a = st.top(); st.pop(); vector<ygorz> b = st.top(); st.pop(); int u = 0, v = 0; /*for (; u < a.size(); u++) { for (; v < b.size()&&b[v] < a[u]; v++); if (v < b.size()&&a[u] == b[v]) c.push_back(a[u]); } */ int wt = 0; while (u < a.size() || v < b.size()) { if (u == a.size()) { // qaq[++wt] = b[v]; v++; } else if (v == b.size()) { // qaq[++wt] = a[u]; u++; } else { if (a[u] < b[v]) { // qaq[++wt] = a[u]; u++; } else if (a[u] > b[v]) { // qaq[++wt] = b[v]; v++; } else { qaq[++wt] = a[u]; u++; v++; } } } vector<ygorz> c; if(wt){ c.clear(); qaq[wt + 1].first = -1; for (int i = 1; i <= wt; i++){ c.push_back(qaq[i]); } } st.push(c); } else { ULL hash = 5; int ll = 2; vector<ygorz> a = st.top(); st.pop(); vector<ygorz> b = st.top(); st.pop(); for (int i = 0; i < a.size(); i++) { hash = hash * power[a[i].second] + a[i].first; ll += a[i].second; ll %= 199193; } hash = hash * power[a.size()] + 7; a.clear(); a.push_back(make_pair(hash, ll)); int u = 0, v = 0; int wt = 0; while (u < a.size() || v < b.size()) { if (u == a.size()) { qaq[++wt] = b[v]; v++; } else if (v == b.size()) { qaq[++wt] = a[u]; u++; } else { if (a[u] < b[v]) { qaq[++wt] = a[u]; u++; } else if (a[u] > b[v]) { qaq[++wt] = b[v]; v++; } else { qaq[++wt] = a[u]; u++; v++; } } } vector<ygorz> c; c.clear(); c.push_back(qaq[1]); for (int i = 2; i <= wt; i++){ c.push_back(qaq[i]); } st.push(c); } printf("%d\n", st.top().size()); } return 0; }