哈希!!!
今天是学的是哈希(hash),主要是(自认为)给数据一个特殊的数字编号用于比较
1.字串
求在中的出现次数,可以用string类自带的.find(字串,开始位置)函数
注意大小写
#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.兔子
哈希模板题来了
题目给定次询问,每次询问需要判断给定的两个子串是否相等
使用哈希完成
这个原理不难,应该没人会讲吧
#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
这道题涉及了 删除哈希 但这个也不难。
首先删除第个可以理解为保留和,因此只要顺便把进制调一下就好了
#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配合哈希模板写出这道题()
#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)
这次要想办法让哈希能够一连判断多次
应此除了普通哈希,还要加一个重置哈希
#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;
}