rolling hash

时间:2022-11-04 20:19:12

也是需要查看,然后修改,rolling hash, recursive hash, polynomial hash, double hash.如果一次不够,那就2次。需要在准备一个线段树,基本的线段树容易些,带lazy标记的区间修改的线段树不是很好写。hash seed key根据需要选择, 我看别人写的,可以写成一个随机数,每次随机选择一个素数作为种子,这样好像好一些。

 #include<bits/stdc++.h>
#define pb push_back
#define FOR(i, n) for (int i = 0; i < (int)n; ++i)
#define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e6 + ;
const int mod1 = 1e9 + ;
const int mod2 = 1e9 + ;
const int arg1 = ;
const int arg2 = ;
int n, k;
string str;
pii H[maxn];
map<pii, int> has[maxn];
map<pii, int> g;
int pw1, pw2;
bool check(map<pii, int> & m) {
for (auto it = m.begin(); it != m.end(); it++) {
if(it->second > ) return ;
else if(g.find(it->first) == g.end()) return false;
}
return ;
}
void solve() {
cin >> n >> k;
cin >> str;
pw1 = pw2 = ;
for (int i = ; i < k - ; i++) {
pw1 = ll(pw1) * arg1 % mod1;
pw2 = ll(pw2) * arg2 % mod2;
}
pii h = pii(, );
for (int i = ; i < k; i++) {
h.first = (ll(h.first) * arg1 + ll(str[i] - 'a' + )) % mod1;
h.second = (ll(h.second) * arg2 + ll(str[i] - 'a' + )) % mod2;
}
H[] = h;
has[][h]++;
for (int i = ; i < n * k; i++) {
h.first = (ll(h.first) - ll(str[i - ] - 'a' + ) * pw1 % mod1 + mod1) % mod1;
h.second = (ll(h.second) - ll(str[i - ] - 'a' + ) * pw2 % mod2 + mod2) % mod2;
int ni = (i + k - ) % (n * k);
h.first = (ll(h.first) * arg1 + ll(str[ni] - 'a' + )) % mod1;
h.second = (ll(h.second) * arg2 + ll(str[ni] - 'a' + )) % mod2;
has[i % k][h]++;
H[i] = h;
}
int m; cin >> m;
string cur;
for (int j = ; j <= m; j++) {
cin >> cur;
pii h = pii(, );
for (int i = ; i < k; i++) {
h.first = (ll(h.first) * arg1 + ll(cur[i] - 'a' + )) % mod1;
h.second = (ll(h.second) * arg2 + ll(cur[i] - 'a' + )) % mod2;
g[h] = j;
}
}
for (int j = ; j < k; j++) if(check(has[j])) {
cout << "YES" << endl;
bool f = ;
for (int i = j; i < n * k; i += k) {
if(f) cout << " ";
cout << g[H[i]]; f = ;
}
cout << endl;
return;
}
cout << "NO" << endl;
}
int main() {
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
solve();
return ;
}