一、简单应用
给出一串数以及一个数字 C,要求计算出所有 A - B = C的数对的个数(不同位置的数字一样的数对算不同的数对)
其实本题可以直接用map存
const int prime = 1000007;
ll a[prime], n, m, ans;
struct node
{
ll x; //当前数字(已经hash)
int y; //数字出现次数
}ha[prime]; //hash表
int find(ll x) //找到x的位置
{
int y = abs(x) % prime; //除法散列法(x可能是负数)
while (ha[y].x != 0 && ha[y].x != x)
{
y = (++y) % prime; //开放寻址法、线性探查
}
return y;
}
void push(ll x)
{
int y = find(x);
ha[y].y++;
ha[y].x = x;
}
int check(ll x)
{
return ha[find(x)].y;
}
void solve()
{
cin >> n >> m;
for (ll i = 0; i < n; i++)
{
cin >> a[i];
push(a[i]);
}
for (ll i = 0; i < n; i++)
ans += check(a[i] - m); //找a-b=m,则在a中找b+m
cout << ans;
}
二、 离散化
离散化,就是把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。在不改变数据相对大小的条件下,对数据进行相应的压缩(例如按排名顺序) 本质是哈希的一种
按位次离散化的方式有两种,一种可以去重,另一种不能去重但是写法相对简单
const int prime = 1000007;
struct node
{
int x, y, e;
friend bool operator<(node a, node b)
{
return a.e > b.e;
}
}a[maxn];
int fa[prime],ha[maxn<<1],n;
int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void init(int len)
{
for (int i = 1; i <= len; i++)
fa[i] = i;
}
void solve()
{
int tot = -1;
memset(ha, 0, sizeof(ha));
memset(a, 0, sizeof(ha));
memset(fa, 0, sizeof(ha));
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].x >> a[i].y >> a[i].e;
ha[++tot] = a[i].x;
ha[++tot] = a[i].y;
}
sort(ha, ha + tot);
int len = unique(ha, ha + tot) - ha; //去重,两个地址相减是长度
for (int i = 1; i <= n; i++)
{
a[i].x = lower_bound(ha, ha + len, a[i].x) - ha; //用其在hash表中的位置代替数值
a[i].y = lower_bound(ha, ha + len, a[i].y) - ha;
}
init(len);
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++)
{ //merge操作
int x = find(a[i].x), y = find(a[i].y);
if (a[i].e) //如果有连等关系,则merge
fa[x] = y;
else if(x==y)
{
cout << "NO" << '\n';
return;
}
}
cout << "YES" << '\n';
}
找逆序对个数
int ha[maxn], a[maxn]; //a是最开始的数组,ha是离散化后的数组
ll c[maxn], n;
inline ll lowbit(ll x)
{
return x & (-x);
}
inline void addone(int x, int y)
{
for (; x <=n; x += lowbit(x))
c[x] += y;
}
inline ll getsum(int x)
{
ll sum = 0;
for (; x; x -= lowbit(x))
sum += c[x];
return sum;
}
inline bool cmp(const int& x, const int& y)
{
if (a[x] == a[y])
return x > y;//避免元素相同
return a[x] > a[y];//按照原序列第几大排列
}
void solve()
{
int temp;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
ha[i] = i;
}
sort(ha + 1, ha + n + 1, cmp);
ll ans = 0;
for (int i = 1; i <= n; i++)
{
addone(ha[i], 1);
ans += getsum(ha[i] - 1);
}
cout << ans;
}
//对应的离散化的另一种写法
bool cmp(int a, int b)
{
return a < b;
}
for (int i = 1; i <= n; i++)
{
cin >> a[i];
ha[i] = -a[i];
}
sort(ha + 1, ha + 1 + n, cmp);
//int cnt = unique(ha + 1, ha + 1 + n) - ha - 1;
for (int i = 1; i <= n; i++)
{
int temp = a[i];
a[i] = lower_bound(ha + 1, ha + 1 + n, -temp) - ha;
}
把区间离散化,需要离散化l,r,r+1
int cnt = 0;
for (int i = 1; i <= n; i++)
{
cin >> x[i] >> y[i];
a[++cnt] = x[i];
a[++cnt] = y[i];
a[++cnt] = y[i] + 1;
}
sort(a + 1, a + cnt + 1);
cnt = unique(a + 1, a + cnt + 1) - (a + 1);
for (int i = 1; i <= n; i++)
{
x[i] = lower_bound(a + 1, a + cnt + 1, x[i]) - a;
y[i] = lower_bound(a + 1, a + cnt + 1, y[i]) - a;
}
三、字符串哈希(用于快速判断子串是否是回文串、字符串匹配)
int p = 131, len; //p的值根据字符的大小范围来设定
unsigned long long pp[maxn], has[maxn], rhas[maxn];
string s;
cin >> s;
len = s.size();
s = ' ' + s;
has[0] = rhas[0] = pp[0] = 1;
for (int i = 1; i <= len; ++i) //处理正反hash值
{
pp[i] = pp[i - 1] * p;
has[i] = has[i - 1] * p + s[i] - 'a';
rhas[i] = rhas[i - 1] * p + s[len - i + 1] - 'a';
}
int check(int l, int r) //判断l,r区间是否是回文串
{
return has[r] - has[l - 1] * (pp[r - l + 1]) ==
rhas[len - l + 1] - rhas[len - r] * pp[r - l + 1];
}
//这里演示手动取模,在被卡hash的时候可以用(可以用unsign long long自动取模)
//但一定要确保所有地方不爆数据范围去取mod,否则会存在两个模数
const ll mod = 1e9 + 7;
long long has1[maxn], has2, pp, p = 30;
string s1, s2;
void solve()
{
cin >> s1 >> s2;
int len1 = s1.size(), len2 = s2.size();
s1 = " " + s1, s2 = " " + s2;
has1[0] = 0, has2 = 0, pp = 1;
for (int i = 1; i <= len2; i++)
pp = pp * p % mod;
for (int i = 1; i <= len1; i++)
{
has1[i] = has1[i - 1] * p % mod + s1[i] - 'A' + 1;
has1[i] %= mod;
}
for (int i = 1; i <= len2; i++)
{
has2 = has2 * p % mod + s2[i] - 'A' + 1;
has2 %= mod;
}
for (int i = 1; i <= len1 - len2 + 1; i++)
{
if (has2 % mod == ((has1[i + len2 - 1] - has1[i - 1] * pp % mod) + mod) % mod)
cout << i << '\n';
}
}