跳到正文

个人记录

8.20总结

Daniel_Chou_hn约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年8月20日
收录
2026年8月21日
更新
2026年11月21日
篇幅
约 8 分钟

版本

  1. v1当前

哈希!!!

今天是学的是哈希(hash),主要是(自认为)给数据一个特殊的数字编号用于比较

1.字串

求aa在bb中的出现次数,可以用string类自带的.find(字串,开始位置)函数
注意大小写

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        string a,b;
        cin>>a>>b;
        for(auto &x:a)x=tolower(x);
        for(auto &x:b)x=tolower(x);
        int i=0,cnt=0;
        while(b.find(a,i)!=-1){
            cnt++;
            i=b.find(a,i)+1;
        }
        cout<<cnt<<'\n';
    }
    return 0;
}

2.兔子

哈希模板题来了
题目给定mm次询问,每次询问需要判断给定的两个子串是否相等

使用哈希完成
这个原理不难,应该没人会讲吧

C++
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
const int N=1e6+10;
const int p=131;
int pw[N],hs[N];
int gethash(int l,int r){
	return hs[r]-hs[l-1]*pw[r-l+1];
}
signed main(){
	string s;
	cin>>s;
	int size=s.size();
	s="X"+s;
	pw[0]=1;
	for(int i=1;i<=size;i++){
		hs[i]=hs[i-1]*p+s[i];
		pw[i]=p*pw[i-1];
	}
	int m;
	cin>>m;
	while(m--){
		int l1,r1,l2,r2;
		cin>>l1>>r1>>l2>>r2;
		int x=gethash(l1,r1),y=gethash(l2,r2);
		if(x==y)cout<<"Yes\n";
		else cout<<"No\n";
	}
}

3.Spelling Check

这道题涉及了 删除哈希 但这个也不难。
首先删除第ii个可以理解为保留1∼(i−1)1\sim (i-1)和(i+1)∼n(i+1) \sim n,因此只要顺便把进制调一下就好了

C++
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
const int N=1e6+10;
const int p=131;
int pw[N],hs[N],hs2[N];
int gethash(int l,int r,int *hs){
	return hs[r]-hs[l-1]*pw[r-l+1];
}
int init(string &s,int *h){
	int size=s.size()-1;
	for(int i=1;i<=size;i++){
		h[i]=h[i-1]*p+s[i];
		pw[i]=p*pw[i-1];
	}
	return size;
}
int delhash(int l,int r,int *h,int len){
	return gethash(1,l-1,h)*pw[len-r]+gethash(r+1,len,h);
}
signed main(){
	string s1,s2;
	cin>>s1>>s2;
	int size1=s1.size();
	int size2=s2.size();
	s1="X"+s1;
	s2="X"+s2;
	pw[0]=1;
	init(s1,hs);
	init(s2,hs2);
	int m;
	cin>>m;
	vector<int>ans;
	for(int i=1;i<=size1;i++){
		int x=delhash(i,i,hs,size1);
		if(x==hs2[size2])ans.push_back(i);
	}
	cout<<ans.size()<<'\n';
	for(auto x:ans)cout<<x<<' ';
	return 0;
}

4.【模板】字符串哈希

这道题需要用双哈希,但我不用~
所以我用map配合哈希模板写出这道题(O(N)O(N))

C++
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
const int N=1e6+10;
const int p=131;
map<int,bool>g;
int pw[N],hs[N],hs2[N];
int gethash(int l,int r,int *hs){
	return hs[r]-hs[l-1]*pw[r-l+1];
}
int init(string &s,int *h){
	int size=s.size()-1;
	for(int i=1;i<=size;i++){
		h[i]=h[i-1]*p+s[i];
		pw[i]=p*pw[i-1];
	}
	return size;
}
int delhash(int l,int r,int *h,int len){
	return gethash(1,l-1,h)*pw[len-r]+gethash(r+1,len,h);
}
signed main(){int n;cin>>n;
	while(n--){
		string s;
		cin>>s;
		s="A"+s;
		init(s,hs);g[gethash(1,s.size()-1,hs)]=1;
	}
	cout<<g.size();
	return 0;
}

5.Three Friends (Day1)

这次要想办法让哈希能够一连判断多次
应此除了普通哈希,还要加一个重置哈希

C++
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
const int N=2e6+5,P=1331;
ull hs[N],power[N];
int n,mid;
string s;
ull gethash(int l,int r){
	return hs[r]-hs[l-1]*power[r-l+1];
}
ull rehash(int l,int r,int pos){
	return gethash(l,pos-1)*power[r-pos]+gethash(pos+1,r);
}
ull lt,rt;
bool check(int pos){
	if(pos==mid){
		lt=gethash(1,pos-1);
		rt=gethash(pos+1,n);
	}
	else if(pos<mid){
		lt=rehash(1,mid,pos);
		rt=gethash(mid+1,n);
	}
	else{
		lt=gethash(1,mid-1);
		rt=rehash(mid,n,pos);
	}
	return lt==rt;
}
map<ull,int>vis;
int main(){
	cin>>n;
	cin>>s;
	s="s"+s;
	mid=(n+1)/2;
	power[0]=1;
	for(int i=1;i<=n;i++){
		power[i]=power[i-1]*P;
		hs[i]=hs[i-1]*P+s[i];
	}
	int mark,ans=0;
	for(int i=1;i<=n;i++){
		if(check(i)==1){
			ull tmp;
			mark=i;
			tmp=rt;
			if(vis[tmp]>0){
				continue;
			}
			vis[tmp]=1;
			ans++;
		}
	}
	if(ans>1){
		cout<<"NOT UNIQUE";
	}
	else if(ans==0)cout<<"NOT POSSIBLE";
	else {
		if(mark<=mid){
			cout<<s.substr(mid+1);
		}
		else cout<<s.substr(1,n/2);
	}
	return 0;
}
原文