哈希
定义
一个把字符串映射到整数的函数 ,这个函数 被称为哈希函数。
例题与示例代码
1. 字符串哈希
1. 单哈希
using ull = unsigned long long;
const ull base = 131;
const ull mod = 1e9 + 7;
inline ull get_hash(string s) {
int len = s.size();
ull res = 0;
for (int i = 0;i < len;i++) res = (res * base + (ull)s[i]) % mod;
return res;
}2. 双哈希
using ull = unsigned long long;
ull base = 131;
ull mod1 = 212370440130137957, mod2 = 1e9 + 7;
inline ull get_hash1(string s) {
int len = s.size();
ull ans = 0;
for (int i = 0; i < len; i++) ans = (ans * base + (ull)s[i]) % mod1;
return ans;
}
inline ull get_hash2(string s) {
int len = s.size();
ull ans = 0;
for (int i = 0; i < len; i++) ans = (ans * base + (ull)s[i]) % mod2;
return ans;
}
inline bool compare(const string s, const string t) {
bool f1 = get_hash1(s) != get_hash1(t);
bool f2 = get_hash2(s) != get_hash2(t);
return f1 || f2;
}2. 字符串哈希plus
增强了数据强度。单哈希可能过不去(我没试过)双哈希轻松过去。
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
ull base = 131;
ull mod1 = 212370440130137957, mod2 = 1e9 + 7;
inline ull get_hash1(string s) {
int len = s.size();
ull ans = 0;
for (int i = 0; i < len; i++) ans = (ans * base + (ull)s[i]) % mod1;
return ans;
}
inline ull get_hash2(string s) {
int len = s.size();
ull ans = 0;
for (int i = 0; i < len; i++) ans = (ans * base + (ull)s[i]) % mod2;
return ans;
}
inline bool compare(const string s, const string t) {
bool f1 = get_hash1(s) != get_hash1(t);
bool f2 = get_hash2(s) != get_hash2(t);
return f1 || f2;
}
const int N = 1e6 + 5;
int n;
string a[N];
namespace Solution {
inline void Main() {
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
sort(a + 1, a + 1 + n);
int ans = 1;
for (int i = 1;i < n;i++) {
if (compare(a[i], a[i + 1])) ans++;
}
cout << ans;
}
}
signed main() {
int T = 1;
while (T--) Solution::Main();
return 0;
}字典树
Code
const int N = 3e6 + 5;
const int L = 66;
class Trie {
private:
inline int calc(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'z') return c - 'a' + 10;
return c - 'A' + 36;
}
public:
int nex[N][L], cnt;
int tot[N];
bool exist[N];
inline void init() {
for (int i = 0;i <= cnt;i++) {
tot[i] = 0, exist[i] = 0;
for (int j = 0;j < L;j++) {
nex[i][j] = 0;
}
}
}
inline void insert(char* s, int l) {
int p = 0;
for (int i = 0;i < l;i++) {
int c = calc(s[i]);
if (!nex[p][c]) nex[p][c] = ++cnt;
p = nex[p][c];
tot[p]++;
}
exist[p] = 1;
}
inline bool find(char* s, int l) {
int p = 0;
for (int i = 0;i < l;i++) {
int c = calc(s[i]);
if (!nex[p][c]) return false;
p = nex[p][c];
}
return exist[p];
}
inline int search(char* s, int l) {
int p = 0;
for (int i = 0;i < l;i++) {
int c = calc(s[i]);
if (!nex[p][c]) return 0;
p = nex[p][c];
}
return tot[p];
}
}trie;例题一 于是他错误地点名开始了
直接套模版就 AC 了。没什么好说的。
namespace Solution {
inline void Main() {
cin >> n;
for (int i = 1;i <= n;i++) {
string s;
cin >> s;
trie.insert(s, (int)s.size());
}
cin >> m;
while (m--) {
string s;
cin >> s;
int ans = trie.find(s, (int)s.size());
if (ans == 0) cout << "WRONG\n";
else if (!mp[s]) mp[s] = 1, cout << "OK\n";
else cout << "REPEAT\n";
}
}
}例题二 最长异或路径
01-trie 是指字符集为 的 trie。01-trie 可以用来维护一些数字的异或和。
支持修改(删除 + 重新插入),和全局加一(即:让其所维护所有数值递增 1,本质上是一种特殊的修改操作)。
首先先看一下这个例题。
题意就是给定一个树,求对于任意两点路径异或和的最大值。
怎么做呢?首先不妨令 表示 和 两点的路径异或和。首先考虑一个东西: 这是显然的。所以我们不妨处理所有的 。其中 代表点集。
接着将这些值塞进 01trie 即可,随后每次查找值的时候显然对于给定的一个数 在 01trie 假设匹配到它的第 位,有两种选择:和它值相同的边,和它值不同的边。
显然选择不同的边。
于是代码也就出来了。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int BIT = 30;
const int M = N * (BIT + 1);
class Trie {
public:
int nex[M][2], tot;
Trie() {
tot = 1;
memset(nex, 0, sizeof(nex));
}
inline void insert(int x) {
int p = 1;
for (int i = BIT;i >= 0;i--) {
int c = (x >> i) & 1;
if (!nex[p][c]) nex[p][c] = ++tot;
p = nex[p][c];
}
}
inline int get(int x) {
int res = 0, p = 1;
for (int i = BIT;i >= 0;i--) {
int c = (x >> i) & 1;
if (nex[p][c ^ 1]) {
res |= (1 << i);
p = nex[p][c ^ 1];
} else {
p = nex[p][c];
}
}
return res;
}
}trie;
int n, ans;
struct Node {
int v, w;
};
vector <Node> g[N];
int dis[N];
inline void add_edge(int u, int v, int w) {
g[u].push_back({ v,w });
g[v].push_back({ u,w });
}
inline void dfs(int u, int fa) {
trie.insert(dis[u]);
ans = max(ans, trie.get(dis[u]));
for (auto [v, w] : g[u]) {
if (v == fa) continue;
dis[v] = dis[u] ^ w;
dfs(v, u);
}
}
namespace Solution {
inline void Main() {
cin >> n;
for (int i = 1;i < n;i++) {
int u, v, w;
cin >> u >> v >> w;
add_edge(u, v, w);
}
dfs(1, 0);
cout << ans;
}
};
signed main() {
int T = 1;
// cin >> T;
while (T--) Solution::Main();
return 0;
}下面给出 01trie 的模版:
const int N = 3e6 + 5;
namespace trie {
constexpr int H = 21;
int ch[N * (H + 1)][2], w[N * (H + 1)], xorv[N * (H + 1)];
int tot = 0;
inline int mknode() {
++tot;
ch[tot][1] = ch[tot][0] = w[tot] = xorv[tot] = 0;
return tot;
}
inline void maintain(int o) {
w[o] = xorv[o] = 0;
if (ch[o][0]) {
w[o] += w[ch[o][0]];
xorv[o] ^= xorv[ch[o][0]] << 1;
}
if (ch[o][1]) {
w[o] += w[ch[o][1]];
xorv[o] ^= (xorv[ch[o][1]] << 1) | (w[ch[o][1]] & 1);
}
w[o] = w[o] & 1;
}
inline void insert(int& o, int x, int dep) {
if (!o) o = mknode();
if (dep > H) return (void)(w[o]++);
insert(ch[o][x & 1], x >> 1, dep + 1);
maintain(o);
}
inline void erase(int o, int x, int dep) {
if (dep > 20) return (void)(w[o]--);
erase(ch[o][x & 1], x >> 1, dep + 1);
maintain(o);
}
inline void addall(int o) {
swap(ch[o][0], ch[o][1]);
if (ch[o][0]) addall(ch[o][0]);
maintain(o);
}
}