跳到正文

个人记录

字符串学习笔记

Bryan1676约 12 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

字符串学习笔记

字符串哈希

给每个字符串一个函数,将它映射到一个 [0,p−1][0,p-1] 的数字上,如果两个字符串的哈希值相等,有很大概率它们两个相等。一般的,字符串的哈希函数取:(p,basep,base 均为质数)

hash=∑i=0lensibasei mod phash=\sum_{i=0}^{len} s_i base^i \bmod p

用途

判断字符串的两个连续子串是否相等

记

hashi=∑j=0isjbasej mod phash_i=\sum_{j=0}^{i} s_j base^j \bmod p

我们有

hash(l,r)=hashr−hashl−1baselhash(l,r)=\frac{hash_r-hash_{l-1}}{base^l}

判断字符串是否是回文串

记

hash′=∑j=0lensjbasei−j mod phash'=\sum_{j=0}^{len} s_j base^{i-j} \bmod p

对于一个字符串,如果

hash′=hashhash'=hash

那么它是回文的(正过来的哈希值等于反过来的哈希值)

例题

T1 [POI 2010] ANT-Antisymmetry

马拉车板子,但也可以二分哈希。

显然长度一定是偶数。

枚举中心点,找最大的边界即可,要求两个哈希:正的和反过来取反后的字符串的,复杂度 O(∣s∣log⁡∣s∣)\mathcal O(|s| \log|s|)

code
C++
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
	cout<<x<<" ";
}
ivoid wln(int x){
	cout<<x<<"\n";
}
ivoid wstr(string s){
	cout<<s<<"\n";
}
#define cout cerr

const int maxn=5e5+10;
const int mod=998244353;
const int base=7891234;
int n,a[maxn],hash1[maxn],hash2[maxn],po[maxn],inv[maxn];
string s;

bool check(int l,int r,int len){
	int h1=(hash1[l]-(hash1[l-len-1]*po[len+1]%mod)+mod)%mod;
	int h2=(hash2[r]-(hash2[r+len+1]*po[len+1]%mod)+mod)%mod;
//	cout<<l<<" "<<r<<" "<<len<<" "<<h1<<" "<<h2<<"\n";
	return (h1==h2);
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>s;
	for(int i=1;i<=n;i++) a[i]=s[i-1]-'0';
	inv[1]=962176176;
	inv[0]=po[0]=1,po[1]=base;
	for(int i=2;i<=n;i++) inv[i]=inv[i-1]*inv[1]%mod,po[i]=po[i-1]*base%mod;
	for(int i=1;i<=n;i++)
		hash1[i]=(hash1[i-1]*base+(a[i]*9178237))%mod;
	for(int i=n;i>=1;i--)
		hash2[i]=(hash2[i+1]*base+((a[i]^1)*9178237))%mod;
	for(int i=1;i<=n;i++){
//		cout<<hash1[i]<<" "<<hash2[i]<<"\n";
	}
	int l=1,r=2;
	int ans=0;
	while(r<=n){
		int l0=0,r0=min(l-1,n-r),m0=(l0+r0)>>1;
		while(r0>l0){
			if(check(l,r,m0)) l0=m0;
			else r0=m0-1;
			m0=(l0+r0+1)>>1;
		}
//		cout<<l<<" "<<r<<" "<<m0<<"\n";
		if(check(l,r,0))ans+=(m0+1);
		l++,r++;
	}
	wln(ans);
	return 0;
}

T2 [POI 2012] OKR-A Horrible Poem

发现对于一个长度为 lenlen 的区间,它的完整周期的长度一定是它的约数,因此考虑枚举长度,现在的问题变成了如何 O(1)\mathcal O(1) 的判断一个长度 dd 的前缀是不是一个字符串的完成周期。

注意到这个直接判 hash(l,r−d)=hash(l+d,r)hash(l,r-d)=hash(l+d,r) 即可。

code
C++
#include<bits/stdc++.h>
using namespace std;

#define ull unsigned long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
	cout<<x<<" ";
}
ivoid wln(int x){
	cout<<x<<"\n";
}
ivoid wstr(string s){
	cout<<s<<"\n";
}
#define cout cerr

#include <cctype>
#include <cstdio>
#include <cstdint>
uint8_t buf[1<<20], *p1, *p2;
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
inline bool blank(const char x) {return !(x^32)||!(x^10)||!(x^13)||!(x^9);}
template<typename Tp> inline void read(Tp &x) {x=0; register bool z=true; register char a=gc(); for(;!isdigit(a);a=gc()) if(a=='-') z=false; for(;isdigit(a);a=gc()) x=(x<<1)+(x<<3)+(a^48); x=(z?x:~x+1);}
inline void read(double &x) {x=0.0; register bool z=true; register double y=0.1; register char a=gc(); for(;!isdigit(a);a=gc()) if(a=='-') z=false; for(;isdigit(a);a=gc()) x=x*10+(a^48); if(a!='.') return x=z?x:-x,void(); for(a=gc();isdigit(a);a=gc(),y/=10) x+=y*(a^48); x=(z?x:-x);}
inline void read(char &x) {for(x=gc();blank(x)&&(x^-1);x=gc());}
inline void read(char *x) {register char a=gc(); for(;blank(a)&&(a^-1);a=gc()); for(;!blank(a)&&(a^-1);a=gc()) *x++=a; *x=0;}
inline void read(string &x) {x=""; register char a=gc(); for(;blank(a)&&(a^-1);a=gc()); for(;!blank(a)&&(a^-1);a=gc()) x+=a;}
template<typename T,typename ...Tp> inline void read(T &x,Tp &...y) {read(x),read(y...);}
template<typename Tp> inline void write(Tp x) {if(!x) return pc(48),void(); if(x<0) pc('-'),x=~x+1; register int len=0; register char tmp[64]; for(;x;x/=10) tmp[++len]=x%10+48; while(len) pc(tmp[len--]);}
inline void write(const double x) {register int a=6; register double b=x,c=b; if(b<0) pc('-'),b=-b,c=-c; register double y=5*powl(10,-a-1); b+=y,c+=y; register int len=0; register char tmp[64]; if(b<1) pc(48); else for(;b>=1;b/=10) tmp[++len]=floor(b)-floor(b/10)*10+48; while(len) pc(tmp[len--]); pc('.'); for(c*=10;a;a--,c*=10) pc(floor(c)-floor(c/10)*10+48);}
inline void write(const pair<int,double>x) {register int a=x.first; if(a<7) {register double b=x.second,c=b; if(b<0) pc('-'),b=-b,c=-c; register double y=5*powl(10,-a-1); b+=y,c+=y; register int len=0; register char tmp[64]; if(b<1) pc(48); else for(;b>=1;b/=10) tmp[++len]=floor(b)-floor(b/10)*10+48; while(len) pc(tmp[len--]); a&&(pc('.')); for(c*=10;a;a--,c*=10) pc(floor(c)-floor(c/10)*10+48);} else cout<<fixed<<setprecision(a)<<x.second;}
inline void write(const char x) {pc(x);}
inline void write(const bool x) {pc(x?49:48);}
inline void write(char *x) {fputs(x,stdout);}
inline void write(const char *x) {fputs(x,stdout);}
inline void write(const string &x) {fputs(x.c_str(),stdout);}
template<typename T,typename ...Tp> inline void write(T x,Tp ...y) {write(x),write(y...);}


const int maxn=5e5+10;
const int base=925309799;
int n,q,a[maxn];
ull hash1[maxn],po[maxn],inv[maxn];
string s;
vector<int> ys[maxn];

inline ull query(int l,int r){
	if(r<l) return 0;
	return 1ull*hash1[r]-(1ull*hash1[l-1]*po[r-l+1]);
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	read(n),read(s);
	for(int i=1;i<=n;i++) a[i]=s[i-1]-'0';
	po[0]=1,po[1]=base;
	for(int i=2;i<=n;i++) po[i]=1ull*po[i-1]*base;
	for(int i=1;i<=n;i++)
		hash1[i]=1ull*hash1[i-1]*base+(1ull*a[i]*966123631294162ull);
	for(int i=1;i<=n;i++){
		for(int j=i;j<=n;j+=i) ys[j].pb(i);
	}
	read(q);
	int l,r;
	while(q--){
		read(l);read(r);
		for(auto u:ys[r-l+1]){
			if(query(l,r-u)==query(l+u,r)){
				write(u);putchar('\n');
				break;
			}
		}
	}
	return 0;
}

T3 [ABC284F] ABCBAC

原文