Skip to content

Instantly share code, notes, and snippets.

@hjroh0315
Created May 14, 2022 02:36
Show Gist options
  • Save hjroh0315/07986608205687d3e7b4fba01421f19b to your computer and use it in GitHub Desktop.
Save hjroh0315/07986608205687d3e7b4fba01421f19b to your computer and use it in GitHub Desktop.
직접 만들어 본 해시테이블
#include<iostream>
#include<iomanip>
#include<utility>
#include<algorithm>
#include<list>
using namespace std;
template<class K, class V, size_t sz>
struct HashTable
{
list<pair<K,V>> data[sz];
hash<K> Hs;
void insert(K key, V val)
{
if(exist(key))value(key)=val;
data[Hs(key)%sz].push_back({key,val});
}
void erase(K key)
{
int hs=Hs(key)%sz;
data[hs].remove_if([&key](pair<K,V> pr)
{
return pr.first==key;
});
}
bool exist(K key)
{
int hs=Hs(key)%sz;
for(auto p:data[hs])if(p.first==key)return 1;
return 0;
}
V& value(K key)
{
int hs=Hs(key)%sz;
for(auto&p:data[hs])if(p.first==key)return p.second;
data[hs].push_back({key,V{}});
return data[hs].back().second;
}
V& operator[](K key)
{
return value(key);
}
};
int main()
{
HashTable<string,int,10007> table;
int n,m;
cin>>n>>m;
while(n--)
{
string s;
cin>>s;
table[s]=1;
}
int ans=0;
while(m--)
{
string s;
cin>>s;
ans+=table.exist(s);
}
cout<<ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment