HUEL_ACM第二学期第一周:字符串hash

B

#include <iostream>
#include <cstring>
#include <cstdio>
//#include <unordered_map>
#include <set>
#include <algorithm>
//#define ll long long
using namespace std;
typedef long long ll;
const ll N=1e6+5,P=131,mod=1e9+7;
ll n,m,p[N],h[N],ha[N];
char a[N],b[N];
void Hash(ll &hash_a){
	p[0]=1;
	h[0]=0;
	ha[0]=0;
	ll len=strlen(b+1);
	for(ll i=1;i<=len;i++){
		h[i]=(h[i-1]*P%mod+b[i])%mod;
		p[i]=p[i-1]*P%mod;
	}
	
	ll len_a=strlen(a+1);
	for(ll i=1;i<=len_a;i++){
		ha[i]=(ha[i-1]*P%mod+a[i])%mod;
	}
	hash_a=ha[len_a];
}
ll get(ll l,ll r){
	return (h[r]%mod-h[l-1]%mod*p[r-l+1]%mod+mod)%mod;
}
int main(){
	ll T;
	scanf("%lld",&T);
	while(T--){
		
		//memset(ha,0,sizeof ha);
		//memset(h,0,sizeof h);
		//memset(p,0,sizeof p);
		
		scanf("%s%s",a+1,b+1);
		ll k;
		Hash(k);
		
		ll ans=0;
		ll len_a=strlen(a+1),len_b=strlen(b+1);
		for(ll l=1;l+len_a-1<=len_b;l++){
			ll r=l+len_a-1;
			if(k==get(l,r))ans++;
		}
		printf("%lld\n",ans);
	}
}

C

#include <iostream>
#include <cstring>
#define int long long
using namespace std;
const int N=1e6+5,P=131,mod=1e9+7;
int h1[N],h2[N],p1[N],p2[N];
char s[N];
void make_hash(){
	int len=strlen(s+1);
	
	p1[0]=1,h1[0]=0;
	for(int i=1;i<=len;i++){
		h1[i]=(h1[i-1]*P%mod+s[i])%mod;
		p1[i]=p1[i-1]*P%mod;
	}
	
	p2[len+1]=1,h2[len+1]=0;
	for(int i=len;i>=1;i--){
		h2[i]=(h2[i+1]*P%mod+s[i])%mod;
		p2[i]=p2[i+1]*P%mod;
	}
}

int get1(int l,int r){
	return (h1[r]%mod-h1[l-1]%mod*p1[r-l+1]%mod+mod)%mod;
}

int get2(int l,int r){
	return (h2[l]%mod-h2[r+1]%mod*p2[r-l+1]%mod+mod)%mod;
}

bool check(int l,int r){
	//cout<<l<<"$$$"<<r<<" "<<get1(l,r)<<" "<<get2(l,r)<<endl;
	if(get1(l,r)==get2(l,r))return true;
	return false;
}

main(){
	while(cin>>s+1){
		make_hash();
		//cout<<s+1<<endl;
		int len=strlen(s+1);
		int pos=0;
		for(int i=1;i<=len;i++){
			if(check(i,len)){
				pos=i;
				//cout<<"****"<<pos<<endl;
				break;
			}
		}
		
		cout<<s+1;
		for(int i=pos-1;i>=1;i--)cout<<s[i];
		cout<<endl;
	}
}

D:回文串分为奇数串和偶数串,先进行扩展变形,将字符串扩展成奇数串(每个原字符间在加一个其他字符,这样只需要考虑奇数串的情况了),然后枚举回文串的中心点,,二分答案。

#include <iostream>
#include <cstring>
#include <algorithm>
//#define int long long
using namespace std;
const int N=2e6+5,P=131,mod=1e9+7;
typedef unsigned long long ULL;
char str[N];
ULL hl[N],hr[N],p[N];
void make_hash(int n){
	p[0]=1;
	for(int i=1,j=n;i<=n;i++,j--){
		hl[i]=(hl[i-1]*P+str[i]-'a'+1);
		hr[i]=(hr[i-1]*P+str[j]-'a'+1);
		p[i]=p[i-1]*P;
	}
}
ULL get(ULL h[],int l,int r){
	return (h[r]-h[l-1]*p[r-l+1]);
}
int main(){
    int T=1;
	while(scanf("%s",str+1),strcmp("END",str+1)){
		int n=strlen(str+1);
		
		for(int i=n*2;i>=1;i-=2){
			str[i]=str[i/2];
			str[i-1]='z'+1;
		}
		n*=2;
		
		make_hash(n);
		
		int res=0;
		for(int i=1;i<=n;i++){
			int l=0,r=min(i-1,n-i);
			while(l<r){
				int mid=l+r+1>>1;
				if(get(hl,i-mid,i+mid)==get(hr,n-(i+mid)+1,n-(i-mid)+1))l=mid;
				else r=mid-1;
			}
			if(str[i-l]<='z')res=max(res,l+1);
			else res=max(res,l);
		}
		
		printf("Case %d: %d\n",T++,res);
	}
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值