跳到正文

个人记录

8.27 字符串哈希测试总结【T4未完结】

3Luby3约 14 分钟
原文
文章信息与版本

文章信息

发布
2026年8月28日
收录
2026年8月28日
更新
2026年11月10日
篇幅
约 14 分钟

版本

  1. v1当前

T1 U585175 DNA

得分 80\texttt{80} 分。

赛时代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=5e6+10;
const unsigned long long p=13331;
unsigned long long hs[maxx];
unsigned long long pw[maxx];
string s;
int k;
unsigned long long get_hash(int l,int r){
	return hs[r]-hs[l-1]*pw[r-l+1];
}
map<unsigned long long,int> mp;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>s;
	s='#'+s;
	cin>>k;
	int len=s.size();
	pw[0]=1;
	for(int i=1;i<=len;i++){
		hs[i]=hs[i-1]*p+s[i];
		pw[i]=pw[i-1]*p;
	}
	int ans=0;
	for(int i=1;i<=len;i++){
		if(i+k-1>len) break;
		unsigned long long h=get_hash(i,i+k-1);
//		cout<<i<<" "<<i+k-1<<" "<<h<<"\n"; 
		mp[h]++;
		ans=max(ans,mp[h]);
	}
	cout<<ans;
	return 0;
} 

剩下两点 TLE。罪魁祸首:map。

改为 unordered_map 即可。

AC CODE
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=5e6+10;
const unsigned long long p=13331;
unsigned long long hs[maxx];
unsigned long long pw[maxx];
string s;
int k;
unsigned long long get_hash(int l,int r){
	return hs[r]-hs[l-1]*pw[r-l+1];
}
unordered_map<unsigned long long,int> mp;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>s;
	s='#'+s;
	cin>>k;
	int len=s.size();
	pw[0]=1;
	for(int i=1;i<=len;i++){
		hs[i]=hs[i-1]*p+s[i];
		pw[i]=pw[i-1]*p;
	}
	int ans=0;
	for(int i=1;i<=len;i++){
		if(i+k-1>len) break;
		unsigned long long h=get_hash(i,i+k-1);
//		cout<<i<<" "<<i+k-1<<" "<<h<<"\n"; 
		mp[h]++;
		ans=max(ans,mp[h]);
	}
	cout<<ans;
	return 0;
} 

T2 P4503 [CTSC2014] 企鹅 QQ

赛时代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=210;
const int N=3e4+10;
const unsigned long long p=13331;
unsigned long long hs[N][maxx];
unsigned long long pw[maxx];
int n,l,c;
unsigned long long get_hash(int id,int l,int r){
	return hs[id][r]-hs[id][l-1]*pw[r-l+1];
}
unsigned long long del_hash(int id,int pos,int l,int r){
	return get_hash(id,pos+1,r)+get_hash(id,l,pos-1)*pw[r-pos+1];
}
void dh(int id,string s){
//	memset(pw,0,sizeof pw);
	pw[0]=1;
	for(int i=1;i<=l;i++){
		hs[id][i]=hs[id][i-1]*p+s[i];
		pw[i]=pw[i-1]*p;
	}
}
int sum(int n){
	int num=0;
	for(int i=1;i<n;i++){
		num+=i;
	}
	return num;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>l>>c;
	for(int i=1;i<=n;i++){
		string s;
		cin>>s;
		s='#'+s;
		dh(i,s);
	}
	int ans=0;
	for(int pos=1;pos<=l;pos++){
		int cnt=0;
		map<int,int> mp;
		map<unsigned long long,bool> mp1;
		for(int i=1;i<=n;i++){
			unsigned long long h=del_hash(i,pos,1,l);
//			cout<<h<<"\n";
			if(mp1[h]==0){
				cnt++; 
				mp1[h]=1; //把哈希值转换为编号 
			} 
			mp[cnt]++; //对应编号次数++ 
		}
		for(int i=1;i<=cnt;i++){
//			cout<<mp[i]<<"\n";
			ans+=sum(mp[i]);
		}
//		cout<<"pos:"<<pos<<" cnt:"<<cnt<<endl;
	}
	cout<<ans;
	return 0;
} 

有 TLE,有 WA,共 20\texttt{20} 分。

思路简单讲一下,因为每个字符串的长度一定,所以从头开始遍历每一个不同的位置,截取每一个字符串剩下部分的哈希值,存入map。最后求和统计。

我这个代码的问题:

  • map 最好改为 unordered map
  • del_hash() 写错了,应从 return get_hash(id,pos+1,r)+get_hash(id,l,pos-1)*pw[r-pos+1]; 改为 return get_hash(id,pos+1,r)+get_hash(id,l,pos-1)*pw[r-pos];
  • 主函数中的统计部分比较麻烦,容易重叠,其实只需要一个 unordered map
AC CODE
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=210;
const int N=3e4+10;
const unsigned long long p=13331;
unsigned long long hs[N][maxx];
unsigned long long pw[maxx];
int n,l,c;
unsigned long long get_hash(int id,int l,int r){
	return hs[id][r]-hs[id][l-1]*pw[r-l+1];
}
unsigned long long del_hash(int id,int pos,int l,int r){
	return get_hash(id,pos+1,r)+get_hash(id,l,pos-1)*pw[r-pos];
}
void dh(int id,string s){
//	memset(pw,0,sizeof pw);
	pw[0]=1;
	for(int i=1;i<=l;i++){
		hs[id][i]=hs[id][i-1]*p+s[i];
		pw[i]=pw[i-1]*p;
	}
}
int sum(int n){
	int num=0;
	for(int i=1;i<n;i++){
		num+=i;
	}
	return num;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>l>>c;
	for(int i=1;i<=n;i++){
		string s;
		cin>>s;
		s='#'+s;
		dh(i,s);
	}
	int ans=0;
	for(int pos=1;pos<=l;pos++){
		int cnt=0;
		unordered_map<unsigned long long,int> mp;
		for(int i=1;i<=n;i++){
			unsigned long long h=del_hash(i,pos,1,l);
			mp[h]++;
		}
		for(auto &p:mp){
			int k=p.second;
			ans+=k*(k-1)/2;
		}
//		cout<<"pos:"<<pos<<" cnt:"<<cnt<<endl;
	}
	cout<<ans;
	return 0;
} 

T3 P8023 [ONTAK2015] Tasowanie

赛时骗分,0 分。

二路归并是指每次从两个数组的当前未取部分的开头选一个元素取出,添加到 T 的末尾,直到两个数组都取完。

每一步比较 A 和 B 当前开头的元素:

  • 若不相等,取较小者。

  • 若相等,则需比较两个剩余后缀的字典序,优先取后缀字典序更小的那个。

    为了快速比较,用字符串哈希预处理两个数组,通过二分找到最长公共前缀,然后比较第一个不同的元素。

  • 若某一个后缀已经取完,则只能取另一个。

AC CODE
C++
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
const int maxx=4e5+10;
const int p=13331;
int n,m,hs_a[maxx],hs_b[maxx],pw[maxx],a[maxx],b[maxx];

void pre(){
    pw[0]=1;
    for(int i=1;i<=400000;i++) pw[i]=pw[i-1]*p;
    for(int i=1;i<=n;i++) hs_a[i]=hs_a[i-1]*p+a[i];
    for(int i=1;i<=m;i++) hs_b[i]=hs_b[i-1]*p+b[i];
}
int get_hsa(int l,int r){ return hs_a[r]-hs_a[l-1]*pw[r-l+1]; }
int get_hsb(int l,int r){ return hs_b[r]-hs_b[l-1]*pw[r-l+1]; }

signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    cin>>m;
    for(int i=1;i<=m;i++) cin>>b[i];
    pre();
    int x=1,y=1;
    while(x<=n && y<=m){
        if(a[x]!=b[y]){
            if(a[x]<b[y]) cout<<a[x++];
            else cout<<b[y++];
        }else{
            int l=0,r=min(n-x+1,m-y+1)+1;
            while(l+1<r){
                int mid=(l+r)/2;
                if(get_hsa(x,x+mid-1)==get_hsb(y,y+mid-1)) l=mid;
                else r=mid;
            }
            int res=l;
            if(x+res-1==n) cout<<b[y++];
            else if(y+res-1==m) cout<<a[x++];
            else{
                if(a[x+res]<b[y+res]) cout<<a[x++];
                else cout<<b[y++];
            }
        }
        cout<<" ";
    }
    while(x<=n) cout<<a[x++]<<" ";
    while(y<=m) cout<<b[y++]<<" ";
    return 0;
}

T4 U594489 山峰

赛时暴力,得 41\texttt{41} 分。

赛时代码
C++
#include<bits/stdc++.h>
using namespace std;
const int maxx=3e5+10;
int n;
int h[maxx];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>h[i];
	}
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			for(int k=j+1;k<=n;k++){
				if(h[i]-h[j]==h[j]-h[k]){
					cout<<"YES";
					return 0;
				}
			}
		}
	}
	cout<<"NO";
	return 0; 
}
原文