题目描述

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 ;
}

最新文章

  1. 重构Web Api程序(Api Controller和Entity) 续篇(2)
  2. struts2 CVE-2012-0392 S2-008 Strict DMI does not work correctly allows remote command execution and arbitrary file overwrite
  3. MongoDB配置文件YAML-based选项全解
  4. 注解:【有连接表的】Hibernate单向N-&gt;1关联
  5. Zabbix点滴
  6. Hydra---Linux下的暴力美学
  7. etc目录名字的意思---挖Linux中的古老缩略语
  8. 批次更新BAPI_OBJCL_CHANGE
  9. Matlab神经网络工具箱学习之二
  10. Android SDK Android NDK 官方下载地址
  11. ALV的报表对用户定义格式的控制(ALV I_SAVE)
  12. php生成随机产生六位数密码的代码
  13. 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)
  14. select、poll、epoll用法
  15. emqtt 试用(八)ssl认证 - 代码验证
  16. WebService开发指南
  17. Graph Cuts学习笔记2014.5.16----1
  18. RAFT实践
  19. maven install中依赖关系打包failed
  20. 从hive导出数据到mysql

热门文章

  1. 利用python和opencv批量去掉图片黑边
  2. jquery添加html代码的几种方法
  3. 人人都会设计模式:观察者模式--Observer
  4. 一个新晋IT行业的努力Duiker
  5. go学习笔记-函数
  6. shell重温---基础篇(流程控制&amp;if判断&amp;for&amp;while&amp;循环操作)
  7. hdu畅通工程(并查集)
  8. 用 Qt 控制 Nikon 显微镜的电动物镜转盘
  9. 实用脚本 4 -- Makefile(不同文件下的多个可执行文件or静态库编译到同一目录下)
  10. Datetime与Datetime2的区别