版本比较
串串学习笔记
正文
增加 141 行删除 10 行
123456789104 行未改动15161718192021222315 行未改动39404142434445464737 行未改动8586878889909192939444 行未改动13914014114214314414514614714 行未改动162163164165# 串串学习笔记## 哈希### 定义一个把字符串映射到整数的函数 $f$,这个函数 $f$ 被称为哈希函数。### 例题与示例代码#### 1. [字符串哈希](https://www.luogu.com.cn/problem/P3370)#### 1. 单哈希```cppusing ull = unsigned long long;const ull base = 131;4 行未改动for (int i = 0;i < len;i++) res = (res * base + (ull)s[i]) % mod;return res;}```##### 2. 双哈希```cppusing ull = unsigned long long;ull base = 131;ull mod1 = 212370440130137957, mod2 = 1e9 + 7;15 行未改动return f1 || f2;}```#### 2. [字符串哈希plus](https://www.luogu.com.cn/problem/U461211)增强了数据强度。单哈希可能过不去(我没试过)双哈希轻松过去。```cpp#include <bits/stdc++.h>using namespace std;37 行未改动while (T--) Solution::Main();return 0;}```## 字典树### Code```cppconst int N = 3e6 + 5;const int L = 66;class Trie {44 行未改动return tot[p];}}trie;```### 例题一 [于是他错误地点名开始了](https://www.luogu.com.cn/problem/P2580)直接套模版就 AC 了。没什么好说的。```cppnamespace Solution {inline void Main() {14 行未改动}}}```1234567894 行未改动14151617181920212215 行未改动38394041424344454637 行未改动8485868788899091929344 行未改动13813914014114214314414514614 行未改动161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296# 哈希## 定义一个把字符串映射到整数的函数 $f$,这个函数 $f$ 被称为哈希函数。## 例题与示例代码### 1. [字符串哈希](https://www.luogu.com.cn/problem/P3370)#### 1. 单哈希```cppusing ull = unsigned long long;const ull base = 131;4 行未改动for (int i = 0;i < len;i++) res = (res * base + (ull)s[i]) % mod;return res;}```#### 2. 双哈希```cppusing ull = unsigned long long;ull base = 131;ull mod1 = 212370440130137957, mod2 = 1e9 + 7;15 行未改动return f1 || f2;}```### 2. [字符串哈希plus](https://www.luogu.com.cn/problem/U461211)增强了数据强度。单哈希可能过不去(我没试过)双哈希轻松过去。```cpp#include <bits/stdc++.h>using namespace std;37 行未改动while (T--) Solution::Main();return 0;}```# 字典树## Code```cppconst int N = 3e6 + 5;const int L = 66;class Trie {44 行未改动return tot[p];}}trie;```## 例题一 [于是他错误地点名开始了](https://www.luogu.com.cn/problem/P2580)直接套模版就 AC 了。没什么好说的。```cppnamespace Solution {inline void Main() {14 行未改动}}}```## 例题二 [最长异或路径](https://www.luogu.com.cn/problem/P4551)01-trie 是指字符集为 ${0,1}$ 的 trie。01-trie 可以用来维护一些数字的异或和。支持修改(删除 + 重新插入),和全局加一(即:让其所维护所有数值递增 `1`,本质上是一种特殊的修改操作)。首先先看一下这个例题。题意就是给定一个树,求对于任意两点路径异或和的最大值。怎么做呢?首先不妨令 $d(u,v)$ 表示 $u$ 和 $v$ 两点的路径异或和。首先考虑一个东西:$d(u,v)=d(rt,u) \oplus d(rt,v)$ 这是显然的。所以我们不妨处理所有的 $u \in P,d(rt,u)$。其中 $P$ 代表点集。接着将这些值塞进 01trie 即可,随后每次查找值的时候显然对于给定的一个数 $x$ 在 01trie 假设匹配到它的第 $i$ 位,有两种选择:和它值相同的边,和它值不同的边。显然选择不同的边。于是代码也就出来了。```cpp#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 的模版:```cppconst 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);}}```