hash

\(k\)进制数转化为\(B\)进制数。

const int B = 131;
typedef unsigned long long ull;
ull ba[Z], ha[Z];
void init(int n)
{
    ba[0] = 1;
    for (int i = 1; i <= n; ++i)
        ba[i] = ba[i - 1] * B;
}
int hash_get(char s[], int n)
{
    ull res = 0;
    for (int i = 1; i <= n; ++i)
        res = res * B + s[i];
    return res;
}
void hash_got(char s[], int n)
{
    for (int i = 1; i <= n; ++i)
        ha[i] = ha[i - 1] * B + s[i];
}
inline int calc(int l, int r) { return ha[r] - ha[l - 1] * ba[r - l + 1]; }
posted @ 2022-10-27 19:12  sandom  阅读(36)  评论(1编辑  收藏  举报