板刷字符串哈希
yuezhongyan · · 个人记录
例1: P3370 【模板】字符串哈希
先放一个模版
听说自然溢出容易被卡,所以重写了一份模数 hash
code
#include <bits/stdc++.h>
#define base 131
#define p1 947947
using namespace std;
int a[1000005];
int hashh(string s) {
int res = 0;
for (int i = 0; i < s.size(); i ++ )
res += res * base + s[i];
return res;
}
int main(void) {
int n; cin >> n;
for (int i = 1; i <= n; i ++ ) {
string s; cin >> s;
a[i] = hashh(s);
}
sort(a + 1, a + n + 1);
cout << unique(a + 1, a + n + 1) - a - 1 << '\n';
return 0;
}
例2:P4391 [BalticOI 2009] Radio Transmission 无线传输
一个练习题。
我们可以把字符串
code
#include <bits/stdc++.h>
#define int long long
#define base 37
#define mod 998244353
using namespace std;
const int N = 1e6 + 5;
int n, bas[N];
char s[N];
signed main(void) {
cin >> n >> (s + 1);
bas[1] = 1;
for (int i = 2; i <= n; i ++ ) bas[i] = bas[i - 1] * base % mod;
int l = 0, r = 0, ans = n;
for (int i = 1; i < n; i ++ ) {
l = (l * base % mod + s[i]);
r = (r + bas[i] * s[n - i + 1] % mod) % mod;
if (l == r) ans = n - i;
}
cout << ans << '\n';
return 0;
}
例3:P4407 [JSOI2009] 电子字典
字符串 hash 实现 trie 树。
这题听说单 hash 会被卡,使用双模数 hash。
字符串长只有20,非常的小,对每个操作暴力做就可以了。
code
#include <bits/stdc++.h>
#define int long long
#define base1 131
#define base2 171
#define mod1 998244353ll
#define mod2 2131517111ll
using namespace std;
typedef pair<int, int> pii;
const int N = 1e4 + 5;
int n, m, st[N];
vector<pii> word;
pii hashh(string s) {
int res1 = 0, res2 = 0;
for (auto it : s) {
res1 = (res1 * base1 % mod1 + it) % mod1;
res2 = (res2 * base2 % mod2 + it) % mod2;
}
return make_pair(res1, res2);
}
bool check(pii x) {
auto pos = lower_bound(word.begin(), word.end(), x);
if (*pos != x || st[pos - word.begin() + 1]) return false;
st[pos - word.begin() + 1] = 1;
return true;
}
string add(string s, int pos, char ch) {
string res = "";
if (pos == -1) res += ch;
for (int i = 0; i < s.size(); i ++ ) {
res += s[i];
if (i == pos) res += ch;
}
return res;
}
string change(string s, int pos, char ch) {
string res = "";
for (int i = 0; i < s.size(); i ++ ) {
if (i != pos) res += s[i];
else res += ch;
}
return res;
}
string del(string s, int pos) {
string res = "";
for (int i = 0; i < s.size(); i ++ )
if (i != pos) res += s[i];
return res;
}
signed main(void) {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) {
string s; cin >> s;
word.push_back(hashh(s));
}
sort(word.begin(), word.end());
while (m -- ) {
for (int i = 1; i <= n; i ++ ) st[i] = 0;
string s; cin >> s;
if (check(hashh(s))) {cout << -1 << '\n'; continue;}
int res = 0;
for (int j = 0; j < 26; j ++ ) {
string t = add(s, -1, char(j + 'a'));
if (check(hashh(t))) res ++ ;
}
for (int i = 0; i < s.size(); i ++ ) {
string t = del(s, i);
if (check(hashh(t))) res ++ ;
for (int j = 0; j < 26; j ++ ) {
t = add(s, i, char(j + 'a'));
if (check(hashh(t))) res ++ ;
t = change(s, i, char(j + 'a'));
if (check(hashh(t))) res ++ ;
}
}
cout << res << '\n';
}
return 0;
}
例4:P4824 [USACO15FEB] Censoring S
字符串 hash 实现 KMP。
可以把前
注意到本题还可能由于删数导致两个串拼接,显然使用栈来维护即可。
#include <bits/stdc++.h>
#define int long long
#define base1 131
#define base2 171
#define mod1 998244353ll
#define mod2 1777771
using namespace std;
typedef pair<int, int> pii;
const int N = 1e6 + 5;
int stk[N], top, bas[N] = {1};
char ans[N];
int hashh(string s) {
int res1 = 0;
for (int i = 0; i < s.size(); i ++ )
res1 = (res1 * base1 % mod1 + s[i]) % mod1;
return res1;
}
signed main(void) {
string s, t; cin >> s >> t;
int cmp = hashh(t);
for (int i = 1; i < N; i ++ ) bas[i] = (bas[i - 1] * base1) % mod1;
for (int i = 0; i < s.size(); i ++ ) {
stk[top + 1] = (stk[top] * base1 % mod1 + s[i]) % mod1;
ans[++ top] = s[i];
if (top >= t.size() && ((stk[top] - stk[top - t.size()] * bas[t.size()] % mod1) % mod1 + mod1) % mod1 == cmp) top -= t.size();
}
for (int i = 1; i <= top; i ++ ) cout << ans[i];
return 0;
}
加入一道不是字符串哈希的题目,但他在 xls 的题单里面。
P3435 [POI 2006] OKR-Periods of Words
注意到本题要求最长周期,类似例2,发现我们要求最短公共前后缀。
我们依旧先求出
code:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
char s[N];
int j, nxt[N], n, ans;
signed main(void) {
cin >> n >> (s + 1);
for (int i = 2; i <= n; i ++ ) {
while (j && s[j + 1] != s[i]) j = nxt[j];
if (s[j + 1] == s[i]) j ++ ;
nxt[i] = j;
}
for (int i = 1; i <= n; i ++ ) {
j = i;
while (nxt[j]) j = nxt[j];
if (nxt[i]) nxt[i] = j;
ans += (i - j);
}
cout << ans << '\n';
return 0;
}