字符串哈希总结

· · 个人记录

1.P3370 【模板】字符串哈希

正常比较两个字符串(字典序)通过 ASCLL 码,需要从左往右找到第一个不相等的来得出大小关系,复杂度高

可以用数字代替字符串,不许用一位位比,直接比较数字大小

而我们使用 hash 函数快速将字符串转为对应的哈希值

但可能出现哈希冲突,即字符串不同,哈希值相同的情况

常见的哈希:

  1. 单射:每个字符串对应一个哈希值,但浪费了没有对应字符串的哈希值
  2. 满射:用完全部哈希值,但存在多个字符串共用一个哈希值的情况
  3. 双射:单射 + 满射,没有浪费也没有冲突

比赛常用进制哈希,把字符串转化为一个 p 进制数

运用 unsigned long long 自然溢出,自动取模

hash函数

int hash1(string s){
    int lens=s.length();
    int sum=0;
    for(int i=0;i<lens;i++){
        sum=sum*p1+s[i];//进制转换
    }
    return sum%N;//单防爆数组
}

Ac Code

#include<bits/stdc++.h>
//自然溢出
#define int unsigned long long
using namespace std;
const int N=1e7+7;
//本题样例卡hash,需要用双哈希,平时不推荐使用
//开两个不一样的进制数
const int p1=131;
const int p2=13331;
int n;
int cnt=0;
int vis1[N],vis2[N];
int hash1(string s){
    int lens=s.length();
    int sum=0;
    for(int i=0;i<lens;i++){
        sum=sum*p1+s[i];
    }
    return sum%N;
}
int hash2(string s){
    int lens=s.length();
    int sum=0;
    for(int i=0;i<lens;i++){
        sum=sum*p2+s[i];
    }
    return sum%N;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    while(n--){
        string s;
        cin>>s;
        int tmp1=hash1(s),tmp2=hash2(s);
        //若两个哈希值都被统计过,说明碰到重复的了
        if(vis1[tmp1]==1&&vis2[tmp2]==1){
            continue;
        }
        cnt++;
        vis1[tmp1]=1;
        vis2[tmp2]=1;
    }
    cout<<cnt;
    return 0;
} 

2.P10468 兔子与兔子

套个前缀和

得出子串哈希

ull hush(int l,int r){
    return hs[r]-hs[l-1]*pw[r-l+1];//类似于前缀和
}

Ac Code

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N=1e7+7;
const ull p=131;
string s;
int n;
ull hs[N],pw[N];
//得出一串字母的哈希值
ull hush(int l,int r){
    return hs[r]-hs[l-1]*pw[r-l+1];//减去的部分乘上位数
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>s;
    int lens=s.length();
    s="#"+s;
    cin>>n;
    pw[0]=1;
    for(int i=1;i<=lens;i++){
        pw[i]=pw[i-1]*p;//统计p的次方
        hs[i]=hs[i-1]*p+s[i];//统计前缀和
    }
    while(n--){
        int l1,r1,l2,r2;
        cin>>l1>>r1>>l2>>r2;
        ull tmp1=hush(l1,r1),tmp2=hush(l2,r2);
        if(tmp1==tmp2){
            cout<<"Yes\n";
        }
        else{
            cout<<"No\n";
        }
    }
    return 0;
} 

3.CF39J Spelling Check

删除操作

ull del_hash(int l,int r,int i){
    //返回l到i-1的位的子串的hash值乘上位数 + i+1到r的位的子串的hash值
    return get_hash(i+1,r)+get_hash(l,i-1)*pw[r-i];
}

Ac Code

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N=1e7+7;
const ull p=131;
vector<int> res;
string s1,s2;
int n;
int total=0;
ull hs1[N],hs2[N],pw[N];
ull get_hash(int l,int r){
    return hs1[r]-hs1[l-1]*pw[r-l+1];
}
ull del_hash(int l,int r,int i){
    return get_hash(i+1,r)+get_hash(l,i-1)*pw[r-i];
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>s1>>s2;
    int len1=s1.length(),len2=s2.length();
    s1="#"+s1;
    s2="#"+s2;
    pw[0]=1;
    for(int i=1;i<=len1;i++){
        pw[i]=pw[i-1]*p;
        hs1[i]=hs1[i-1]*p+s1[i];
    }
    for(int i=1;i<=len2;i++){
        hs2[i]=hs2[i-1]*p+s2[i];
    }
    ull tmpb=hs2[len2];
    //打擂台,逐个删除比较是否相等
    for(int i=1;i<=len1;i++){
        if(del_hash(1,len1,i)==tmpb){
            total++;//统计方案数
            res.push_back(i);//存答案
        }
    }
    cout<<total<<'\n';
    for(int i=0;i<total;i++){
        cout<<res[i]<<' ';
    }
    return 0;
}