模拟散列表 - C++ STL 6种写法

哈希表

unordered_set

运行时间:149 ms

#include <iostream>
#include <unordered_set>
using namespace std;

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int q;
    cin >> q;
    unordered_set<int> s;
    while(q--)
    {
        char op;
        int x;
        cin >> op >> x;
        if(op == 'I') s.insert(x);
        else cout << (s.count(x)? "Yes": "No") << '\n';
    }
    return 0;
}

unordered_map

运行时间:189 ms

#include <iostream>
#include <unordered_map>
using namespace std;

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int q;
    cin >> q;
    unordered_map<int, bool> m;
    while(q--)
    {
        char op;
        int x;
        cin >> op >> x;
        if(op == 'I') m[x] = 1;
        else cout << (m[x]? "Yes": "No") << '\n';
    }
    return 0;
}

gp_hash_table(G++ 编译器特有)

运行时间:116 ms

#include <iostream>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {
    inline int operator ()(int x) const { return x ^ RANDOM; }
};

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int q;
    cin >> q;
    gp_hash_table<int, null_type, chash> s;
    while(q--)
    {
        char op;
        int x;
        cin >> op >> x;
        if(op == 'I') s.insert(x);
        else cout << (s.find(x) == s.end()? "No": "Yes") << '\n';
    }
    return 0;
}

平衡树(默认红黑树)

set
运行时间:221 ms

#include <iostream>
#include <set>
using namespace std;

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int q;
    cin >> q;
    set<int> s;
    while(q--)
    {
        char op;
        int x;
        cin >> op >> x;
        if(op == 'I') s.insert(x);
        else cout << (s.count(x)? "Yes": "No") << '\n';
    }
    return 0;
}

map

运行时间:262 ms

#include <iostream>
#include <map>
using namespace std;

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int q;
    cin >> q;
    map<int, bool> m;
    while(q--)
    {
        char op;
        int x;
        cin >> op >> x;
        if(op == 'I') m[x] = 1;
        else cout << (m[x]? "Yes": "No") << '\n';
    }
    return 0;
}

pb_ds/tree(G++ 编译器特有)

运行时间:159 ms

#include <iostream>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int q;
    cin >> q;
    tree<int, null_type> s;
    while(q--)
    {
        char op;
        int x;
        cin >> op >> x;
        if(op == 'I') s.insert(x);
        else cout << (s.find(x) == s.end()? "No": "Yes") << '\n';
    }
    return 0;
}

总结

STL(非 pb_ds)的数据结构普遍常数较大,但使用方便,无特殊需求使用 unordered_set 即可。

关于神奇的 pb_ds:OI Wiki 的介绍

运行时间:gp_hash_table < unordered_set < __gnu_pbds::tree < unordered_map < set < map

  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值