仅提供代码,请自行理解,若有不懂之处可以私信我。
// 手写拉链
#include <vector>
#include <string>
#include <iostream>
const int MAX = 1e5 + 10;
const int HASH = 1e7 + 7;
const int OFFSET = 2e9 + 5;
const int A = 263, B = 997;
#define hash(x) ((((263*1LL*((x%OFFSET+OFFSET)%OFFSET)+997)%HASH+HASH)%HASH)%MAX)
#define insert(x) hash_table[hash(x)].emplace_back(x)
std::vector<int> hash_table[MAX];
bool find(int x) {
for (auto a : hash_table[hash(x)])
if (a == x) return true;
return false;
}
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int N, x; std::string op;
for (std::cin >> N; N; --N) {
std::cin >> op >> x;
if (op == "I") insert(x);
else std::cout << (find(x) ? "Yes\n" : "No\n");
}
std::cout.flush();
return 0;
}
// STL unordered_map
#include <string>
#include <iostream>
#include <unordered_map>
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int N, x; std::string op;
std::unordered_map<int, int> set;
for (std::cin >> N; N; --N) {
std::cin >> op >> x;
if (op == "I") set[x] = 1;
else std::cout << (set[x] ? "Yes\n" : "No\n");
}
std::cout.flush();
return 0;
}
// STL map
#include <map>
#include <string>
#include <iostream>
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int N, x; std::string op;
std::map<int, int> set;
for (std::cin >> N; N; --N) {
std::cin >> op >> x;
if (op == "I") set[x] = 1;
else std::cout << (set[x] ? "Yes\n" : "No\n");
}
std::cout.flush();
return 0;
}
// STL set
#include <set>
#include <string>
#include <iostream>
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int N, x; std::string op;
std::set<int> set;
for (std::cin >> N; N; --N) {
std::cin >> op >> x;
if (op == "I") set.insert(x);
else std::cout << (set.find(x) != set.end() ? "Yes\n" : "No\n");
}
std::cout.flush();
return 0;
}
// pb_ds gp_hash_table
#include <string>
#include <iostream>
#include <bits/extc++.h>
int main() {
std::cin.tie(0)->sync_with_stdio(0);
__gnu_pbds::gp_hash_table<int, int> set;
int N, x; std::string op;
for (std::cin >> N; N; --N) {
std::cin >> op >> x;
if (op == "I") set[x] = 1;
else std::cout << (set[x] ? "Yes\n" : "No\n");
}
std::cout.flush();
return 0;
}
// pb_ds cc_hash_table
#include <string>
#include <iostream>
#include <bits/extc++.h>
int main() {
std::cin.tie(0)->sync_with_stdio(0);
__gnu_pbds::cc_hash_table<int, int> set;
int N, x; std::string op;
for (std::cin >> N; N; --N) {
std::cin >> op >> x;
if (op == "I") set[x] = 1;
else std::cout << (set[x] ? "Yes\n" : "No\n");
}
std::cout.flush();
return 0;
}
// STL std::unordered_set
#include <iostream>
#include <string>
#include <unordered_set>
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int N; std::string op; int x;
std::unordered_set<int> set;
for (std::cin >> N; N; --N) {
std::cin >> op >> x;
if (op == "I") set.insert(x);
else std::cout << (set.find(x) == set.end() ? "No\n" : "Yes\n");
}
std::cout.flush();
return 0;
}
实测 __gnu_pbds::gp_hash_table
是最快的