跳到正文

个人记录

pbds中hash_table

libohan0905约 2 分钟
原文
文章信息与版本

文章信息

发布
2022年10月10日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 2 分钟

版本

  1. v1当前

hash_table食用方法

引入头文件:

C++
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>

命名空间:

C++
using namespace __gnu_pbds;

定义:

C++
gp_hash_table<string,int>h;

或者

C++
cc_hash_table<string,int>h;

使用:

C++
h["abcde"]=12345;

A:它能干嘛呢?

B:就和map差不多,支持[ ]和find

A:那不如直接用map了

B:不不不,map的总时间复杂度是 O(nlogn) 的,而hash_table的总时间复杂度仅为 O(n)

比如:

P3879 [TJOI2010] 阅读理解

C++
#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
cc_hash_table<string,bitset<1005> >cch;
string s;
int main() {
	int n,m;
	cin>>n;
	for(int i=1; i<=n; i++) {
		int l;
		cin>>l;
		for(int j=1; j<=l; j++) {
			cin>>s;
			cch[s][i]=1;
		}
	}
	cin>>m;
	while(m--) {
		cin>>s;
		for(int i=1; i<=n; i++) {
			if(cch[s][i]) cout<<i<<" ";
		}
		puts("");
	}
}

pbds牛逼!就是背不下来

原文