算法竞赛进阶指南——0x14【Hash】


在这里插入图片描述

Hash表

hash表又称为散列表,与链表结构共同实现,主要两个操作:

  • 计算hash函数的值
  • 定位到对应链表依次遍历、比较hash函数设计的好,可以避免hash冲突
  • 【建立一个大小==值域的数组进行统计和映射,这是最简单的hash思想】

牛客一个小栗子Snowflake Snow Snowflakes

#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const int N = 100010;
const int M = 2010000;
const int mod = 99991;
const int inf = 0x3f3f3f3f;
const int eps = 1e-6;
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
inline ll read()
{
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while ('0' <= c && c <= '9')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
int n, tot;
int a[10], head[N], nex[N], val[N], snow[N][6];
bool f;
inline int H(int *a) //散列函数,计算hash值
{
    int plu = 0, mul = 1;
    for (int i = 0; i < 6; i++)
    {
        plu = (plu + a[i]) % mod;
        mul = (ll)mul * a[i] % mod;
    }
    return (plu + mul) % mod;
}
inline bool same(int *a, int *b)
{
    for (int i = 0; i < 6; i++)
    {
        for (int j = 0; j < 6; j++)
        {
            bool f = 1;
            for (int k = 0; k < 6; k++) //两个里面满足一个即可【顺时针】
                if (a[(i + k) % 6] != b[(j + k) % 6])
                    f = 0;
            if (f)
                return 1;
            f = 1;
            for (int k = 0; k < 6; k++) //两个里面满足一个即可【逆时针】
                if (a[(i + k) % 6] != b[(j - k + 6) % 6])
                    f = 0;
            if (f)
                return 1;
        }
    }
    return 0;
}
inline bool insert(int *a)
{
    int x = H(a); //遍历表头head[x]指向的链表
    for (int i = head[x]; i; i = nex[i])
        if (same(snow[i], a)) //比较
            return 1;
    memcpy(snow[++tot], a, 6 * sizeof(int)); //数组copy函数
    nex[tot] = head[x];
    head[x] = tot;
    return 0;
}
int main()
{
    IOS;
    cin >> n;
    while (n--)
    {
        for (int j = 0; j < 6; j++)
            cin >> a[j];
        if (insert(a))
        {
            puts("Twin snowflakes found.");
            return 0;
        }
    }
    puts("No two snowflakes are alike.");
    return 0;
}

字符串Hash

取固定值p,m,把字符串看作p进制数,并分配一个>0的数代表每种字符,求出该p进制数对m的余数作为hash值【很难冲突】

  • Hash[S + c] ={Hash[S]*p + value[c]}%m
  • Hash[T] ={Hash[S+T]-Hash[S]*p^(length(T))}%m
    可以在O(n)时间内预处理hash值,在O(1)时间内查询hash值【一般取p=131或13331,m取2^64】用usigned long long存储hash可以自动对2^64取模,避免低效的mod运算

牛客一个小栗子兔子与兔子

#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
const int N = 1e6 + 10;
const int M = 2010000;
const int mod = 2147483647;
const int inf = 0x3f3f3f3f;
const int eps = 1e-6;
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
char s[N];
int m, l1, l2, r1, r2;
ull h[N], p[N];
inline ll fast_pow(ll a, ll b)
{ //不要轻易%mod
    ll res = 1;
    for (; b; b >>= 1)
    {
        if (b & 1)
            res = res * a;
        a = a * a;
    }
    return res;
}
inline void H() //hash函数求值
{
    int len = strlen(s + 1);
    // p[0] = 1;
    for (int i = 1; i <= len; i++) //1~i的hash值
    {
        h[i] = h[i - 1] * 131 + (s[i] - 'a' + 1);
        // p[i] = p[i - 1] * 131;
    }
}
int main()
{
    IOS;
    cin >> s + 1;
    cin >> m;
    H();
    while (m--)
    {                                //1~i:h[i]=h[i-1]*p+s[i]***l~r:h[l~r]=h[r]-h[l-1]*p^(r-l+1)
        cin >> l1 >> r1 >> l2 >> r2; //区间hash值
        if (h[r1] - h[l1 - 1] * fast_pow(131, r1 - l1 + 1) == h[r2] - h[l2 - 1] * fast_pow(131, r2 - l2 + 1))
            // if (h[r1] - h[l1 - 1] * p[r1 - l1 + 1] == h[r2] - h[l2 - 1] * p[r2 - l2 + 1])
            puts("Yes");
        else
            puts("No");
    }
    return 0;
}

manachar算法求解最长回文串

牛客一道小栗子Palindrome

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
const int N = 1e6 + 10;
const int M = 3e6 + 10;
const int mod = 2147483647;
const int inf = 0x3f3f3f3f;
const int eps = 1e-6;
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0)
char s[N], str[N << 1];
int tot;
int len[N << 1];
inline int manachar() //马拉车算法求解字符串的最长回文串
{
    int m = strlen(s + 1);      //注意字符串以下标1开头
    str[0] = '@', str[1] = '#'; //防止越界
    for (int i = 1; i <= m; i++)
    { //构造新的字符串,使得有奇有偶的字符串统一为奇字符串
        str[i << 1] = s[i];
        str[i << 1 | 1] = '#';
    }
    int n = m << 1 | 1; //新的字符串长度为2*m+1
    int mid, mx = 0, ans = 0;
    for (int i = 1; i <= n; i++)
    {
        len[i] = (mx > i ? min(mx - i, len[2 * mid - i]) : 1); //这里要分情况考虑,最终得出结论都是取最小值
        while (str[i - len[i]] == str[i + len[i]])             //判断当前点是不是最长回文子串,不断向右拓展
            len[i]++;
        if (i + len[i] > mx) //更新mx,mx为最右边
        {
            mx = i + len[i];
            mid = i; //更新中间点
        }
        ans = max(ans, len[i] - 1); //注意要len[i]-1,len是最长回文串的一半,包括了自己i
    }
    return ans;
}
int main()
{
    // IOS;
    while (cin >> s + 1, strcmp(s + 1, "END"))      //strcmpchar类型的字符串的比较函数
        printf("Case %d: %d\n", ++tot, manachar()); //注意这种形式的输出最好用printf,cout牛客过不了
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WTcrazy _

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值