【字符串】字符串哈希
课堂练习
-P8835 [传智杯 #3 决赛] 子串-
思路
- 纯暴力
Code
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7,LOG=60;
int T,n,m;
string s,t;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>T;
while(T--)
{
int ans=0;
cin>>n>>m;
cin>>s>>t;
//全部转小写
for(int i=0;i<s.size();i++)
{
if(s[i]>='A' && s[i]<='Z') s[i]=(char)s[i]+32;
}
for(int i=0;i<t.size();i++)
{
if(t[i]>='A' && t[i]<='Z') t[i]=(char)t[i]+32;
}
for(int i=0;i<m;i++)
{
if(t[i]==s[0])
{
string x=t.substr(i,s.size());
//字符串.substr(初始位置,需截取长度)
if(x==s) ans++;//统计答案
}
}
cout<<ans<<"\n";
}
return 0;
}-P10468 兔子与兔子-
思路
- 字符串哈希之子串哈希模板
数组定义
- 表示字符串前 个字符组成的前缀的哈希值
- 基底 的 次方,用于子串哈希的位数对齐
- 题目需要判断两个子串是否一样,那么就需要计算字符串中区间 的子串的哈希值
- 计算公式:
- 原理:前缀哈希是从左到右按高位计算的, 对应的是前 位的哈希值,乘以 相当于把它左移到和 相同的高位,再相减就得到了中间子串独立的哈希值
- 有了哈希值,我们现在就只需要判断两个子串的哈希值是否相等即可
Code
#include<bits/stdc++.h>
#define int unsigned long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=131;
int n;
int hs[N],pw[N];
string s;
int get_hash(int l,int r)
{
return hs[r]-hs[l-1]*pw[r-l+1];
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>s>>n;
int len=s.size();
s='@'+s;
pw[0]=1;
for(int i=1;i<=len;i++)
{
hs[i]=hs[i-1]*p+s[i];
pw[i]=pw[i-1]*p;
}
while(n--)
{
int l1,r1,l2,r2;
cin>>l1>>r1>>l2>>r2;
int tmp1=get_hash(l1,r1),tmp2=get_hash(l2,r2);
cout<<((tmp1==tmp2)? "Yes":"No")<<"\n";
}
return 0;
}-CF39J Spelling Check-
思路
- 另一个子串哈希模板
数组定义
- 表示字符串前 个字符组成的前缀的哈希值
- 基底 的 次方,用于子串哈希的位数对齐
- 字符串 的总哈希值
int get_hash(int l,int r)
{
return hs[r]-hs[l-1]*pw[r-l+1];
}计算字符串 中区间子串 的哈希值,详见上一题
int del_hash(int l,int r,int len)
{
return get_hash(1,l-1)*pw[len-r]+get_hash(r+1,len);
}功能:计算字符串 删除区间 后,剩余字符串的哈希值
原理:将剩余字符串拆分为前半段 与后半段 ,把前半段的哈希值左移后半段长度位(乘以 ),再加上后半段的哈希值,完成两段的拼接,即可得到删除后的完整哈希
- 有了删除操作后,就只需遍历字符串 的每一个位置,计算删除该位置后的哈希值,若与字符串 的哈希相等,则该位置为合法删除位,加入答案,最后按题目要求输出即可
Code
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=131;
int sum;
int hs[N],pw[N];
string s,t;
vector<int> ans;
int get_hash(int l,int r)
{
return hs[r]-hs[l-1]*pw[r-l+1];
}
int del_hash(int l,int r,int len)
{
return get_hash(1,l-1)*pw[len-r]+get_hash(r+1,len);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>s>>t;
int len1=s.size(),len2=t.size();
s='@'+s;
t='@'+t;
pw[0]=1;
for(int i=1;i<=len1;i++)
{
hs[i]=hs[i-1]*p+s[i];
pw[i]=pw[i-1]*p;
}
for(int i=1;i<=len2;i++) sum=sum*p+t[i];
for(int i=1;i<=len1;i++)
{
int tmp=del_hash(i,i,len1);
if(tmp==sum) ans.push_back(i);
}
cout<<ans.size()<<"\n";
for(int x:ans) cout<<x<<" ";
return 0;
}-P3370 【模板】字符串哈希-
思路
Code-暴力
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7,LOG=60;
int n,ans;
string s;
map<string,int> mp;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
while(n--)
{
cin>>s;
mp[s]++;
}
for(auto x:mp)
{
if(x.second!=0) ans++;
}
cout<<ans;
return 0;
}Code-手写哈希
#include<bits/stdc++.h>
#define int unsigned long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e6+7,LOG=60;
const int p=131;
int n,ans;
string s;
map<int,int> mp;
void init(string x)
{
int len=s.size();
s='@'+s;
int sum=0;
for(int i=1;i<=len;i++) sum=sum*p+s[i];
mp[sum]=1;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>s;
init(s);
}
for(auto x:mp)
{
if(x.second!=0) ans++;
}
cout<<ans;
return 0;
}-P6739 [BalticOI 2014] Three Friends (Day1)-
思路
我们可以枚举被删掉的点 ,看剩下的字符串是否可以分成相同的两个字符串,分三种情况讨论点 :
- (删掉中点)
lt=get_hash(1,x-1);//取左半边哈希值
rt=get_hash(x+1,n);//取右半边哈希值- (删掉左半边里面的一个字符)
lt=del_hash(1,mid,x);//删除位置x之后剩下左半边字符串的哈希值
rt=get_hash(mid+1,n);//取右半边哈希值- (删掉右半边里面的一个字符)
lt=get_hash(1,mid-1);//取左半边哈希值
rt=del_hash(mid,n,x);//删除位置x之后剩下右半边字符串的哈希值Code
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=2e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=13331;
int n,mid,idx,cnt;
int lt,rt;
int hs[N],pw[N];
string s;
map<int,int> vis;
int get_hash(int l,int r)
{
return hs[r]-hs[l-1]*pw[r-l+1];
}
int del_hash(int l,int r,int pos)
{
return get_hash(l,pos-1)*pw[r-pos]+get_hash(pos+1,r);
}
bool check(int x)
{
if(x==mid)
{
lt=get_hash(1,x-1);
rt=get_hash(x+1,n);
}
else if(x<mid)
{
lt=del_hash(1,mid,x);
rt=get_hash(mid+1,n);
}
else
{
lt=get_hash(1,mid-1);
rt=del_hash(mid,n,x);
}
return (lt==rt);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>s;
int len=s.size();
s='@'+s;
mid=(n+1)/2;
pw[0]=1;
for(int i=1;i<=len;i++)
{
hs[i]=hs[i-1]*p+s[i];
pw[i]=pw[i-1]*p;
}
for(int i=1;i<=n;i++)
{
if(check(i))//若删除i后字符串左右两边相等
{
idx=i;
if(vis[rt]>0) continue;//同一个位置只记录一次
vis[rt]=1;
cnt++;//统计答案
}
}
if(cnt==0) cout<<"NOT POSSIBLE";
else if(cnt>1) cout<<"NOT UNIQUE";
else cout<<((idx<=mid)? s.substr(mid+1):s.substr(1,n/2));//判断删掉的位置小于或大于中点来判断取左半边还是右半边
return 0;
}