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;
}