F有没有做卡哈希
原本都是统一用的131,但是过不了,我把键值改成*一个瞎打的数字后就过了
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef unsigned long long ull;
const ll N = 1e5 + 5;
const ll mod = 1e9 + 7;
typedef double db;
const double eps = 1e-6;
#define endl '\n'
#define PII pair<ll, ll>
#define fi first
#define se second
// 我的哈希值 *131 +23317
ll n, m, k;
// ll ans;
ll q;
queue<pair<ull, ll>> op;
map<ull, ll> all, ans;
map<ull, ll> times;
vector<ll> lens;
ll hasLens[10005];
ull h[N];
ull hx[N];
string s;
void init()
{
h[0] = 1;
for (ll i = 1; i < N; i++)
{
h[i] = h[i - 1] * 131;
}
for (ll i = 1; i <= n; i++)
{
hx[i] = hx[i - 1] * 131 + s[i];
}
}
ull has(string s)
{
ull res = 0;
for (char c : s)
{
res = res * 131 + c;
}
return res;
}
ull hax(ll l, ll r)
{
return hx[r] - hx[l - 1] * h[r - l + 1];
}
void solve()
{
cin >> n;
cin >> q;
cin >> s;
s = " " + s;
init();
while (q--)
{
string t;
cin >> t;
cin >> k;
ull res = has(t);
op.push({res, k});
if (!hasLens[t.size()])
{
lens.push_back(t.size());
hasLens[t.size()] = 1;
}
ull temp = res * 124235 + k * 32;
all[temp] = 1;
times[res] = 0;
ans[temp] = -1;
}
for (ll i = 1; i <= n; i++)
{
for (ll j = 0; j < lens.size(); j++)
{
// cout << 111 << " " << j << endl;
ll len = lens[j];
if (len > i)
continue;
ull res = hax(i - len + 1, i);
if (times.count(res))
{
ull temp = ++times[res] * 32 + res * 124235;
if (all.count(temp))
{
ans[temp] = i;
}
}
}
}
// cout << hax(1, 2) << " " << has("ab") << endl;
// for (vector<ll> t : all)
// {
// for (ll t1 : t)
// {
// cout << t1 << " ";
// }
// cout << endl;
// }
while (!op.empty())
{
ull t;
ll k;
t = op.front().first;
k = op.front().second;
op.pop();
// ull res = t;
cout << ans[t * 124235 + k * 32] << endl;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1; // cin>>t;
while (t--)
solve();
return 0;
}