题目描述

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

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

  1. f(si)=ti f(s_{i})=t_{i} f(si​)=ti​ for any index i i i ,
  2. for any character there is exactly one character that f(x)=y f(x)=y f(x)=y ,
  3. for any character there is exactly one character that f(x)=y f(x)=y 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 m m queries characterized by three integers x,y,len x,y,len x,y,len ( 1<=x,y<=n−len+1 1<=x,y<=n-len+1 1<=x,y<=n−len+1 ). For each query check if two substrings s[x... x+len−1] s[x...\ x+len-1] s[x... x+len−1] and s[y... y+len−1] s[y...\ y+len-1] s[y... y+len−1] are isomorphic.

输入输出格式

输入格式:

The first line contains two space-separated integers n n n and m m m ( 1<=n<=2⋅105 1<=n<=2·10^{5} 1<=n<=2⋅105 , 1<=m<=2⋅105 1<=m<=2·10^{5} 1<=m<=2⋅105 ) — the length of the string s s s and the number of queries.

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

The following m m m lines contain a single query on each line: xi x_{i} xi​ , yi y_{i} yi​ and leni len_{i} leni​ ( 1<=xi,yi<=n 1<=x_{i},y_{i}<=n 1<=xi​,yi​<=n , 1<=leni<=n−max(xi,yi)+1 1<=len_{i}<=n-max(x_{i},y_{i})+1 1<=leni​<=n−max(xi​,yi​)+1 ) — the description of the pair of the substrings to check.

输出格式:

For each query in a separate line print "YES" if substrings s[xi... xi+leni−1] s[x_{i}...\ x_{i}+len_{i}-1] s[xi​... xi​+leni​−1] and s[yi... yi+leni−1] s[y_{i}...\ y_{i}+len_{i}-1] s[yi​... yi​+leni​−1] are isomorphic and "NO" otherwise.

输入输出样例

输入样例#1:

7 4
abacaba
1 1 1
1 4 2
2 1 3
2 4 3
输出样例#1:

YES
YES
NO
YES

说明

The queries in the example are following:

  1. substrings "a" and "a" are isomorphic: f(a)=a f(a)=a f(a)=a ;
  2. substrings "ab" and "ca" are isomorphic: f(a)=c f(a)=c f(a)=c , f(b)=a f(b)=a f(b)=a ;
  3. substrings "bac" and "aba" are not isomorphic since f(b) f(b) f(b) and f(c) f(c) f(c) must be equal to a a a at same time;
  4. substrings "bac" and "cab" are isomorphic: f(b)=c f(b)=c f(b)=c , f(a)=a f(a)=a f(a)=a , f(c)=b f(c)=b f(c)=b .
 
Solution:
  本题还是HRZ学长讲的算法,也是贼有意思。
  题意中的相似就并不在意字符具体是什么,而在于字符的排列情况是否能一致,而若我们用$26$个字母分别为关键字弄出$26$个$0,1$串,那么一个字符串的相对位置关系是可以用这$26$个$0,1$排列来唯一确定。
  于是,我们对整个字符串分别以$26$个字母为关键字进行hash,解释一下关键字意思是比如$a$字符为关键字,那么所有非$a$的字符所对应的$K$进制位为$0$,只有为该关键字的位为$1$,这样就能保证相似的排列对应的hash值一致,而整个hash过程也就$O(n)$递推一遍就好了。
  然后对于每次询问,我们就将两段字符串的$26$个hash值取出来,分别用$a,b$数组存下来,从小到大排序,然后比较是否相等即可。
  注意本题hack模数$998244353$,所以我改用了$19260817$做单模数,当然更建议用多模数减少错误率。
代码:
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=,M=,mod2=,mod1=;
int n,m,x,y,len;
ll f[N][],sum[N],a[],b[];
char s[N]; il int gi(){
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-')x=getchar();
if(x=='-')x=getchar(),f=;
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return f?-a:a;
} il bool check(){
For(i,,)
a[i]=(f[x+len-][i]-f[x-][i]*sum[len]%mod1+mod1)%mod1,
b[i]=(f[y+len-][i]-f[y-][i]*sum[len]%mod1+mod1)%mod1;
sort(a+,a+),sort(b+,b+);
For(i,,) if(a[i]!=b[i])return ;
return ;
} int main(){
n=gi(),m=gi();
scanf("%s",s+);
sum[]=;
For(i,,n) {
sum[i]=(sum[i-]*M)%mod1;
For(j,,) f[i][j]=(f[i-][j]*M%mod1+(s[i]=='a'+j-?:))%mod1;
}
while(m--){
x=gi(),y=gi(),len=gi();
(check())?puts("YES"):puts("NO");
}
return ;
}

CF985F Isomorphic Strings的更多相关文章

  1. CF985F Isomorphic Strings (字符串Hash,巧解)

    题目链接 题意翻译 给你一个长度为 \(n\) 的字符串,\(m\) 次询问. 问两个相同长度的子串是否匹配. 我们称两个子串是匹配的,当且仅当其满足: 其中一个子串的字母可替代另一个子串的字母 例如 ...

  2. [LeetCode] Isomorphic Strings

    Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...

  3. leetcode:Isomorphic Strings

    Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...

  4. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  5. [leetcode]205. Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  6. Codeforces 985 F - Isomorphic Strings

    F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...

  7. Educational Codeforces Round 44 (Rated for Div. 2) F - Isomorphic Strings

    F - Isomorphic Strings 题目大意:给你一个长度为n 由小写字母组成的字符串,有m个询问, 每个询问给你两个区间, 问你xi,yi能不能形成映射关系. 思路:这个题意好难懂啊... ...

  8. CodeForces985F -- Isomorphic Strings

    F. Isomorphic Strings time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  9. LeetCode 205. 同构字符串(Isomorphic Strings)

    205. 同构字符串 205. Isomorphic Strings

随机推荐

  1. Redis安装与简单配置

    一.Redis介绍 1.redis是什么? remote dIctionary server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis提 ...

  2. web前端总结面试问题<经常遇到的手写代码>

    冒泡排序 var arr = [5,8,3,6,9] for(var i=0;i<arr.length;i++){ for(var j=i+1;j<arr.length;j++){ if( ...

  3. pyqt4学习资料

    官方文档: http://pyqt.sourceforge.net/Docs/PyQt4/classes.html 啄木鸟社区:https://wiki.woodpecker.org.cn/moin/ ...

  4. is 和 isinstance的区别 and issubclass

    定义一个子类和父类 class A: pass class B(A): pass is print(type(b) is B) # 结果: True print(type(b) is A) # 结果: ...

  5. laravel -- 路由

    基本路由 Route::get('/get',function (){ return "this is get"; }); Route::post('/post',function ...

  6. Hive优化之谓词下推

    Hive优化之谓词下推 解释 Hive谓词下推(Predicate pushdown) 关系型数据库借鉴而来,关系型数据中谓词下推到外部数据库用以减少数据传输 基本思想:尽可能早的处理表达式 属于逻辑 ...

  7. 长沙Uber优步司机奖励政策(3月28日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. 13 IO多路复用 (未完成)

    IO多路复用 6.select版-TCP服务器:最多1024 import select import socket import sys server = socket.socket(socket. ...

  9. 不同编译器下,定义一个地址按x字节对齐的数组

    以前一直用MDK,用__align(4)就可以定义一个首地址被4整除.地址按4字节对齐的数组,但今天用IAR发现这么写编译报错. 搜了一下才发现,原来不同的编译器,需要用不同的表达方式: #if de ...

  10. python操作nosql数据库之memcache

    一.memcache的安装 1.memcache简介 Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象减少读取数据库的次数,从而 ...