CF985F (2020.3.12训练C题)

这篇博客探讨了如何判断两个给定的字符串子串是否具有合法的映射关系,即它们是否是同构的。通过使用哈希函数转换字符串为01串,并构建hashh二维数组来存储每个位置特定字符的哈希值,然后比较两个子串的哈希值数组是否对应相等,从而确定它们是否同构。文章提供了具体的算法思路和AC代码实现。
摘要由CSDN通过智能技术生成

Description:
You are given a string s of length n consisting of lowercase English letters.

For two given strings s and t, say S is the set of distinct characters of s and T is the set of distinct characters of t. The strings s and t are isomorphic if their lengths are equal and there is a one-to-one mapping (bijection) f between S and T for which f(si) = ti. Formally:

f(si) = ti for any index i,
for any character there is exactly one character that f(x) = y,
for any character there is exactly one character that f(x) = y.
For example, the strings “aababc” and “bbcbcz” are isomorphic. Also the strings “aaaww” and “wwwaa” are isomorphic. The following pairs of strings are not isomorphic: “aab” and “bbb”, “test” and “best”.

You have to handle m queries characterized by three integers x, y, len (1 ≤ x, y ≤ n - len + 1). For each query check if two substrings s[x… x + len - 1] and s[y… y + len - 1] are isomorphic.

Input:
The first line contains two space-separated integers n and m (1 ≤ n ≤ 2·105, 1 ≤ m ≤ 2·105) — the length of the string s and the number of queries.

The second line contains string s consisting of n lowercase English letters.

The following m lines contain a single query on each line: xi, yi and leni (1 ≤ xi, yi ≤ n, 1 ≤ leni ≤ n - max(xi, yi) + 1) — the description of the pair of the substrings to check.

Output:
For each query in a separate line print “YES” if substrings s[xi… xi + leni - 1] and s[yi… yi + leni - 1] are isomorphic and “NO” otherwise.

题意:
给定一个长度为n的字符串,和m次起点分别为l和r的长度为len的合法与否询问,结果输出YES或NO(即从s[l]到s[l+len-1]的子串和从s[r]到s[r+len-1]的子串是否合法映射)

我们可以将利用哈希函数将原串构造成01串,进制为base(自己定),定义一个hashh二维数组,
hashh[i][j](比如hash[i][1]表示第i个位置是字母a的哈希值,如果是a,该位置为1,不是为0)

比如aabbc和cceef两个子串,因为如果映射合法,必然a对应c,b对应e,c对应f

所以对两个子串分别从a~z的26种情况计算哈希值并存入a[26]和b[26],排序后,如果映射合法,两个数组一定每个位置一一对应相等,有一处不同则不合法

ac代码如下

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;

const ll base = 131</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值