CodeForces - 985F Isomorphic Strings
假如两个区间的26的字母出现的位置集合分别是 A1,B1,A2,B2,....., 我们再能找到一个排列p[] 使得 A[i] = B[p[i]] ,那么就可以成功映射了。
显然集合可以直接hash,排序一下hash值就可以判断是否匹配了。。。。
虽然不知道为什么要卡19260817,但反正我是坚决不写双hash的,最后随便换了一个大模数(甚至都不是质数2333)然后就过了。。。
Discription
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 Tfor 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] ands[yi... yi + leni - 1] are isomorphic and "NO" otherwise.
Example
7 4
abacaba
1 1 1
1 4 2
2 1 3
2 4 3
YES
YES
NO
YES
Note
The queries in the example are following:
- substrings "a" and "a" are isomorphic: f(a) = a;
- substrings "ab" and "ca" are isomorphic: f(a) = c, f(b) = a;
- substrings "bac" and "aba" are not isomorphic since f(b) and f(c) must be equal to a at same time;
- substrings "bac" and "cab" are isomorphic: f(b) = c, f(a) = a, f(c) = b.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=200005,ha=998244357,b=233;
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
int H[maxn][26],ci[maxn],A[26],B[26];
int n,Q,X,Y,L;
char ch; inline void Get(int *x,int y){
for(int i=0;i<26;i++) x[i]=add(H[y+L-1][i],ha-H[y-1][i]*(ll)ci[L]%ha);
} inline void solve(){
int i;
while(Q--){
scanf("%d%d%d",&X,&Y,&L);
Get(A,X),Get(B,Y); sort(A,A+26),sort(B,B+26); for(i=0;i<26;i++) if(A[i]!=B[i]) break;
puts(i==26?"YES":"NO");
}
} int main(){
scanf("%d%d",&n,&Q); ci[0]=1;
for(int i=1;i<=n;i++) ci[i]=(ci[i-1]*(ll)b)%ha; for(int i=1;i<=n;i++){
ch=getchar();
while(ch<'a'||ch>'z') ch=getchar(); for(int j=0;j<26;j++) H[i][j]=(H[i-1][j]*(ll)b)%ha;
H[i][ch-'a']++;
} solve(); return 0;
}
最新文章
- springBoot上传文件大小设置
- C#-WebForm-★ 制作图片验证码 ★
- 3016 质子撞击炮 II
- ArcGIS地图文档MXD效率慢的一点建议(二)
- Wafer管芯数量及成本估算
- Axure RP中线条的设置
- centOS设为文本启动方式
- javascript中函数声明与函数表达式的区别
- C# Post和Get请求
- 【原】spring boot添加cros全局过滤器
- python+eclipse+pydev开发环境搭建
- 算法第四版jar包下载地址
- python特色_字典,元组,列表
- jdbc配置Spring
- 详细解读简单的lstm的实例
- 使用 resizableImageWithCapInsets 方法实现可伸缩图片
- learning docker steps(5) ----- docker stack 初次体验
- 【Linux 命令】 rsync 目录覆盖软链接,保持软链接不变并同步目录内容
- MongoDB图形化管理工具Toad Mac Edition
- 腾讯云CMQ消息队列在Linux环境下的使用
热门文章
- Linux认知之旅【05 进一步了解Linux装软件】!
- python学习_循环结构 and 类型判断
- java 计算精度处理
- 阻塞&;&;非阻塞
- JVM垃圾回收机制GC
- Using Let’s Encrypt for free SSL Certs with Netscaler
- [Codeforces438E][bzoj3625] 小朋友和二叉树 [多项式求逆+多项式开根]
- BZOJ3456 城市规划 【分治NTT】
- 小L的占卜
- 第二届360杯全国大学生信息安全技术大赛部分解题思路(WEB安全)