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;
}
CodeForces - 985F Isomorphic Strings的更多相关文章
- 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 ...
- Codeforces 985 F - Isomorphic Strings
F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...
- Educational Codeforces Round 44 (Rated for Div. 2) F - Isomorphic Strings
F - Isomorphic Strings 题目大意:给你一个长度为n 由小写字母组成的字符串,有m个询问, 每个询问给你两个区间, 问你xi,yi能不能形成映射关系. 思路:这个题意好难懂啊... ...
- [LeetCode] Isomorphic Strings
Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...
- leetcode:Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- CodeForces985F -- Isomorphic Strings
F. Isomorphic Strings time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- LeetCode_205. Isomorphic Strings
205. Isomorphic Strings Easy Given two strings s and t, determine if they are isomorphic. Two string ...
随机推荐
- Java基础-5运算符
一).算数运算符: 算术运算符的功能是做各种算术运算,其操作数可以是字符型.整型或浮点型数据. 运算符 运算 示例 结果 备注 + 加 5+5 10 - 减 4-2 2 * 乘 2*3 6 既 ...
- unity生命周期
1.静态构造函数 当程序集被加载的时候就被调用了,如果你的unity处于编辑状态时,此时你保存一个脚本(从而迫使重新编译),静态构造函数会立即被调用,因为unity加载了DLL.并且它将不会再次运行, ...
- Opencv3.0.0安装包
这个资源是Opencv3.0.0安装包,包括Windows软件包,Android软件包,IOS软件包,还有opencv的源代码:需要的下载吧. 点击下载
- HDU 4472 Count (DP)
题目:问n个节点构成完全对称的树有多少种方法. 因为树是完全对称的,所以它的子树也是完全对称的. 对于每个树,拿出一个根节点,枚举剩下的节点能拆分成多少个子树. #include <cstdio ...
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 M: 当总统
http://acm.ocrosoft.com/problem.php?cid=1316&pid=12 题目描述 小明想当丑国的总统,丑国大选是按各州的投票结果来确定最终的结果的,如果得到超过 ...
- 实用JS系列——事件类型
事件就是用户对窗口上各种组件的操作.JS中的事件中的事件即由访问Web页面的用户引起的一系列的操作.一般用于浏览器和用户操作进行交互,例如:用户的单击事件等. 类型分为: 内联模型.脚本模型和DOM2 ...
- 思梦PHP-阿里大鱼手机验证码
小伙伴是否做PC网站的时候,是否遇到过注册用户需要使用短信验证的功能呢?或者找回密码,以及验证用户的信息等等功能!今天思梦PHP就为大家带来ThinkPHP整合阿里大鱼短信验证的功能! 首先,我们要明 ...
- 重写Android相机适配不同的设备,对于相机旋转角度问题解决方案
Android开发中经常需要重写相机,由此会导致一些旋转的情况(不同的设备摄像头角度是不一样的),此处按照解决思路给出解决方案: 情形一:只需要旋转摄像头方向以及最终的照片,注意两者需要保持一致 1. ...
- BZOJ3456 城市规划 【分治NTT】
题目链接 BZOJ3456 题解 据说这题是多项式求逆 我太弱不会QAQ,只能\(O(nlog^2n)\)分治\(NTT\) 设\(f[i]\)表示\(i\)个节点的简单无向连通图的数量 考虑转移,直 ...
- margin百分比,重叠和auto
一. margin百分比 1. 普通元素的百分比margin都是相对于容器的宽度计算 2. 绝对定位元素的百分比margin是相对于第一个定位祖先元素(relative/absolute/fixed) ...