题解:P12197 Hash Killer I
yuhong056
·
·
题解
题解:P12197 Hash Killer I
对于自然溢出 Hash,我们可以分情况讨论。
情况 1
很明显,当字符串足够长,Hash 的前面几项 Hash 值都会变为 $0$。
所以只需要在字符串后有足够多的 $\texttt a$ 即可。
## 情况 2
$base$ 不可以被 $2$ 整除。
在这种情况下,我们可以使用“Thue Morse 序列”。
定义 $!s$ 为 $s$ 二进制反转后得到的串。
我们可以构造一个字符串序列,满足 $s_i = s_{i - 1} + !s_{i - 1}$( $+$ 代表字符串连接)。
取 $s_{12}$ 和 $!s_{12}$ 即可得到冲突。
至于为什么是 $12$ 可参考 [oiwiki](https://oiwiki.33dai.wiki/string/hash/#b-%E4%B8%BA%E5%A5%87%E6%95%B0)。
$L$ 则是 $s_{12}$ 的长度 $\div 2$。
## Code
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 1 << 12;
cout << n << ' ' << n / 2 << '\n';
for (int i = 0; i < n; i++) {
cout << char('a' + (__builtin_popcount(i) & 1)); // 构造 Thue Morse 序列
}
return 0;
}
```