假如两个区间的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:

  1. f(si) = ti for any index i,
  2. for any character  there is exactly one character  that f(x) = y,
  3. 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: xiyi 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

Input
7 4
abacaba
1 1 1
1 4 2
2 1 3
2 4 3
Output
YES
YES
NO
YES

Note

The queries in the example are following:

  1. substrings "a" and "a" are isomorphic: f(a) = a;
  2. substrings "ab" and "ca" are isomorphic: f(a) = cf(b) = a;
  3. substrings "bac" and "aba" are not isomorphic since f(b) and f(c) must be equal to a at same time;
  4. substrings "bac" and "cab" are isomorphic: f(b) = cf(a) = af(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的更多相关文章

  1. 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 ...

  2. Codeforces 985 F - Isomorphic Strings

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

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

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

  4. [LeetCode] Isomorphic Strings

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

  5. leetcode:Isomorphic Strings

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

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

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

  7. CodeForces985F -- Isomorphic Strings

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

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

    205. 同构字符串 205. Isomorphic Strings

  9. LeetCode_205. Isomorphic Strings

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

随机推荐

  1. shell sort 排序大讨论

    转自http://roclinux.cn 本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. === [正文开始]有时候学 ...

  2. SqlHelper——数据库小助手

    SqlHelper其实就是一个类. 早就听说过"SqlHelper"这个名词,也查过相关的资料,但还是一头雾水.当真的去实践去用它时,就会发现其实它没那么神秘. 当敲第一个窗体的时 ...

  3. [SDOI2017][bzoj4817] 树点涂色 [LCT+线段树]

    题面 传送门 思路 $LCT$ 我们发现,这个1操作,好像非常像$LCT$里面的$Access$啊~ 那么我们尝试把$Access$操作魔改成本题中的涂色 我们令$LCT$中的每一个$splay$链代 ...

  4. 了解Spark源码的概况

    本文旨在帮助那些想要对Spark有更深入了解的工程师们,了解Spark源码的概况,搭建Spark源码阅读环境,编译.调试Spark源码,为将来更深入地学习打下基础. 一.项目结构 在大型项目中,往往涉 ...

  5. POJ2417 Discrete Logging | A,C互质的bsgs算法

    题目: 给出A,B,C 求最小的x使得Ax=B  (mod C) 题解: bsgs算法的模板题 bsgs 全称:Baby-step giant-step 把这种问题的规模降低到了sqrt(n)级别 首 ...

  6. 旅行规划(travel)

    题目描述 OIVillage 是一个风景秀美的乡村,为了更好的利用当地的旅游资源,吸引游客,推动经济发展,xkszltl 决定修建了一条铁路将当地 nnn 个最著名的经典连接起来,让游客可以通过火车从 ...

  7. (转)Ant使用例子

    文章来自:http://www.blogjava.net/feng0801/archive/2014/07/29/416297.html Ant是一个Apache基金会下的跨平台的构件工具,它可以实现 ...

  8. 牛客小白月赛4——H-相邻的糖果

    链接:https://www.nowcoder.com/acm/contest/134/H来源:牛客网 题目描述 有n个盒子摆成一排,每个盒子内都有ai个糖果. 现在你可以执行以下操作: ·你可以选择 ...

  9. 【转】shell脚本写的俄罗斯方块游戏

    亲测一个很好玩的shell脚本写的俄罗斯方块游戏,脚本来自互联网 先来讲一下思维流程 一.方块的表示 由于shell不能定义二维数组,所以只能用一维数组表示方块,俄罗斯方块主要可以分为7类,每一类方块 ...

  10. 解决_CRT_SECURE_NO_WARNINGS 警告

    问题:我们在程序中使用fopen等CRT函数,就会出现一些警告信息,很烦人,如下: 1>e:/project/htt/ishow/functions.cpp(156) : warning C49 ...