CF858F Isomorphic Strings(Hash)

题目链接:http://codeforces.com/contest/985/problem/F
https://acm.njupt.edu.cn/problem/CF985F
在这里插入图片描述在这里插入图片描述在这里插入图片描述
题意:
给出两个相同长度的字符串,其中a串每一个x都有唯一的f(x)=y转化b串,同时b串中每一个y都有唯一的f(y)=x转化为a串
解题思路:
利用Hash,对26个字母在字符串中的位置赋予Hash值
下面我直接引用我看到的一篇题解:
https://blog.csdn.net/hqg_ac/article/details/85246661
在这里插入图片描述
也就是给的两个子串中,相同出现顺序性的字母必须成对出现
还有就是Hash的时候,我取p=31 37时WA掉了
取p=17 47 131 147等等值AC了
不是很明白(莫非是巧合?)

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define ll unsigned long long
const int mod=1e9+7;
const int p=37;
ll Hash[220000][27];
ll Pow[220000];
int n,m;
int L,R,len;
char s[220000];
void get_hash()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=26;j++)
		{
			Hash[i][j]=(Hash[i-1][j]*p+(s[i]==('a'+j-1)))%mod;
		}
	}
}
int check()
{
	ll a[27];
	ll b[27];
	for(int i=1;i<=26;i++)
	{
		a[i]=(Hash[L+len-1][i]+mod-Hash[L-1][i]*Pow[len]%mod)%mod;
		b[i]=(Hash[R+len-1][i]+mod-Hash[R-1][i]*Pow[len]%mod)%mod;
	}
	sort(a+1,a+27);
	sort(b+1,b+27);
	for(int i=1;i<=26;i++)
		if(a[i]!=b[i])
			return 0;
	return 1;
}
int main()
{
	cin>>n>>m;
	Pow[0]=1;
	for(int i=1;i<=n;i++)
		Pow[i]=Pow[i-1]*p%mod;
	scanf("%s",s+1);
	get_hash();
	while(m--)
	{
		scanf("%d%d%d",&L,&R,&len);
		if(check())
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Buyi.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值