当前位置: 代码网 > it编程>编程语言>C/C++ > 字符串哈希常用板子

字符串哈希常用板子

2024年07月28日 C/C++ 我要评论
字符串哈希可在O(nlogn)的时间复杂度里求kmp的next数组,Z函数,以及马拉车的mlc数组,适合在无资料的考场上DIY。

字符串哈希可在 o ( n l o g n ) o(nlogn) o(nlogn) 的时间复杂度里求kmp的next数组,z函数,以及马拉车的mlc数组,适合在无资料的考场上diy

最好用的字符串双哈希(带反哈希判断回文串)

struct string_hash{
    const int nn;
    const ll base1 = 29, mod1 = 1e9 + 7;
    const ll base2 = 131, mod2 = 1e9 + 9;
    vector<ll> ha1, ha2, pow1, pow2;
    vector<ll> rha1, rha2;
    string_hash(const string &ss,int n1) : nn(n1), ha1(nn + 1), ha2(nn + 1), pow1(nn + 1), pow2(nn + 1), rha1(nn + 1), rha2(nn + 1)
    {
        pow1[0] = pow2[0] = 1;
        for(int i = 1; i <= nn; i++)
        {
            pow1[i] = pow1[i - 1] * base1 % mod1;
            pow2[i] = pow2[i - 1] * base2 % mod2;
        }
        for(int i = 1; i <= nn; i++)
        {
            ha1[i] = (ha1[i - 1] * base1 + ss[i - 1]) % mod1;
            ha2[i] = (ha2[i - 1] * base2 + ss[i - 1]) % mod2;
            rha1[i] = (rha1[i - 1] * base1 + ss[nn - i]) % mod1;
            rha2[i] = (rha2[i - 1] * base2 + ss[nn - i]) % mod2;
        }
    }
    pair<ll, ll> get_hash(int l,int r)
    {
        ll res1 = ((ha1[r] - ha1[l - 1] * pow1[r - l + 1]) % mod1 + mod1) % mod1;
        ll res2 = ((ha2[r] - ha2[l - 1] * pow2[r - l + 1]) % mod2 + mod2) % mod2;
        return {res1, res2};
    }
    pair<ll, ll> get_rhash(int l, int r)
    {
        ll res1 = ((rha1[n - l + 1] - rha1[n - r] * pow1[r - l + 1]) % mod1 + mod1) % mod1;
        ll res2 = ((rha2[n - l + 1] - rha2[n - r] * pow2[r - l + 1]) % mod2 + mod2) % mod2;
        return {res1, res2};
    }
    bool is_palindrome(int l, int r)//判断ss[l, r]是否为回文串
    {
        return get_hash(l, r) == get_rhash(l, r);
    }
    pair<ll, ll> add(pair<ll, ll> aa,pair<ll, ll> bb)
    {
        ll res1 = (aa.f + bb.f) % mod1;
        ll res2 = (aa.s + bb.s) % mod2;
        return {res1, res2};
    }
    pair<ll, ll> mul(pair<ll, ll> aa, ll kk) //aa *= base的k次方
    {
        ll res1 = aa.f * pow1[kk] % mod1;
        ll res2 = aa.s * pow2[kk] % mod2;
        return {res1, res2};
    }
    pair<ll, ll> pin(int l1, int r1, int l2, int r2) //拼接字符串 r1 < l2  ss = s1 + s2
    {
        return add(mul(get_hash(l2, r2), r1 - l1 + 1), get_hash(l1, r1));
    }
};

普通字符串双哈希

struct string_hash{
    const int nn;
    const ll base1 = 29, mod1 = 1e9 + 7;
    const ll base2 = 131, mod2 = 1e9 + 9;
    vector<ll> ha1, ha2, pow1, pow2;
    string_hash(const string &ss, int n1) : nn(n1), ha1(nn + 1), ha2(nn + 1), pow1(nn + 1), pow2(nn + 1)
    {
        pow1[0] = pow2[0] = 1;
        for(int i = 1; i <= nn; i++)
        {
            pow1[i] = pow1[i - 1] * base1 % mod1;
            pow2[i] = pow2[i - 1] * base2 % mod2;
        }
        for(int i = 1; i <= nn; i++)
        {
            ha1[i] = (ha1[i - 1] * base1 + ss[i - 1]) % mod1;
            ha2[i] = (ha2[i - 1] * base2 + ss[i - 1]) % mod2;
        }
    }
    pair<ll, ll> get_hash(int l, int r)
    {
        ll res1 = ((ha1[r] - ha1[l - 1] * pow1[r - l + 1]) % mod1 + mod1) % mod1;
        ll res2 = ((ha2[r] - ha2[l - 1] * pow2[r - l + 1]) % mod2 + mod2) % mod2;
        return {res1, res2};
    }
    bool same(int l1, int r1, int l2, int r2)
    {
        return get_hash(l1, r1) == get_hash(l2, r2);
    }
    pair<ll, ll> add(pair<ll, ll> aa,pair<ll, ll> bb)
    {
        ll res1 = (aa.f + bb.f) % mod1;
        ll res2 = (aa.s + bb.s) % mod2;
        return {res1, res2};
    }
    pair<ll, ll> mul(pair<ll, ll> aa, ll kk) //aa *= base的k次方
    {
        ll res1 = aa.f * pow1[kk] % mod1;
        ll res2 = aa.s * pow2[kk] % mod2;
        return {res1, res2};
    }
    pair<ll, ll> pin(int l1, int r1, int l2, int r2) //拼接字符串 r1 < l2  ss = s1 + s2
    {
        return add(mul(get_hash(l2, r2), r1 - l1 + 1), get_hash(l1, r1));
    }
};

普通字符串双哈希(最快的版本)

const int maxn = 5000010;
char s1[maxn];
ll ha1[maxn], pow1[maxn], rha1[maxn];
struct string_hash{
    const int nn;
    const ll base1 = 29, mod1 = 1e9 + 7;
    string_hash(int n1) : nn(n1)
    {
        pow1[0] = 1;
        for(int i = 1; i <= nn; i++)
            pow1[i] = pow1[i - 1] * base1 % mod1;
        for(int i = 1; i <= nn; i++)
        {
            ha1[i] = (ha1[i - 1] * base1 + s1[i - 1]) % mod1;
            rha1[i] = (rha1[i - 1] * base1 + s1[nn - i]) % mod1;
        }
    }
    int get_hash(int l,int r)
    {
        int res1 = ((ha1[r] - ha1[l - 1] * pow1[r - l + 1]) % mod1 + mod1) % mod1;
        return res1;
    }
    int get_rhash(int l, int r)
    {
        int res1 = ((rha1[n - l + 1] - rha1[n - r] * pow1[r - l + 1]) % mod1 + mod1) % mod1;
        return res1;
    }
    bool same(int l1, int r1, int l2, int r2)
    {
        return get_hash(l1, r1) == get_hash(l2, r2);
    }
    ll add(ll aa,ll bb)
    {
        ll res = (aa + bb) % mod1;
        return res;
    }
    ll mul(ll aa, ll kk) //aa *= base的k次方
    {
        ll res = aa * pow1[kk] % mod1;
        return res;
    }
    ll pin(int l1, int r1, int l2, int r2) //拼接字符串 r1 < l2  ss = s1 + s2
    {
        return add(mul(get_hash(l2, r2), r1 - l1 + 1), get_hash(l1, r1));
    }
};
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com