F. Isomorphic Strings
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 T for 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] and s[yi... yi + leni - 1] are isomorphic and "NO" otherwise.

Example
input
Copy
7 4
abacaba
1 1 1
1 4 2
2 1 3
2 4 3
output
Copy
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.

AC代码为:

#include<iostream>
#include<queue>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
const ll MOD=1e9+7;
ll h[26][maxn],x=107,px[maxn];
char s[maxn];
int main()
{
    int n,m;
    scanf("%d%d", &n,&m);
    scanf("%s", s+1);
    px[0]=1;
    for(int i = 1; i <= n; ++i) px[i]=px[i-1]*x%MOD;
    for(int i = 0; i < 26; ++i)
    {
        for(int j = 1; j <= n; ++j) h[i][j]=(h[i][j-1]*x+int(s[j] == (i+'a')))%MOD;
    }
    while(m--)
    {
        int x,y,l;
        scanf("%d%d%d", &x,&y,&l);
        vector<int> p,q;
        for(int i = 0; i < 26; ++i)
        {
            p.push_back(((h[i][x+l-1]-px[l]*h[i][x-1]%MOD)%MOD+MOD)%MOD);
            q.push_back(((h[i][y+l-1]-px[l]*h[i][y-1]%MOD)%MOD+MOD)%MOD);
        }
        sort(q.begin(),q.end());sort(p.begin(),p.end());
        printf("%s\n", p==q? "YES":"NO");
    }
    return 0;
}

CodeForces985F -- Isomorphic Strings的更多相关文章

  1. [LeetCode] Isomorphic Strings

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

  2. leetcode:Isomorphic Strings

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

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

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

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

  5. Codeforces 985 F - Isomorphic Strings

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

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

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

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

    205. 同构字符串 205. Isomorphic Strings

  8. LeetCode_205. Isomorphic Strings

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

  9. 【刷题-LeetCode】205. Isomorphic Strings

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

随机推荐

  1. maven(1)

    Maven进价:Maven的生命周期阶段 一.Maven的生命周期 Maven的生命周期就是对所有的构建过程进行抽象和统一.包含了项目的清理.初始化.编译.测试.打包.集成测试.验证.部署和站点生成等 ...

  2. SqlServer2005 查询 第六讲 null

    今天们来讲sql命令中的这个null参数 null null: 可以理解成[没有值,空值]的意思 注意以下几点 --1.零和null是不一样的,null表示空值,而零表示的一个确定的值 --2.nul ...

  3. 修改 processor.php 文件,监听用户对该应用的消息

    修改 processor.php 文件,监听用户对该应用的消息 class cgc_fdModuleProcessor extends WeModuleProcessor { public funct ...

  4. hdu 1269 迷宫城堡 (tarjan)

    迷宫城堡Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  5. 官宣!Amazon EMR正式支持Apache Hudi

    ​Apache Hudi是一个开源的数据管理框架,其通过提供记录级别的insert, update, upsert和delete能力来简化增量数据处理和数据管道开发.Upsert指的是将记录插入到现有 ...

  6. word使用指南(经常更新)

    一.快捷键 Ctrl+C 复制 Ctrl+X 剪切 Ctrl+V 粘贴 Ctrl+F 查找 Ctrl+A 全选 Ctrl+Z/Y 撤销/还原撤销 Ctrl+D 打开字体对话框 Ctrl+S 另存为 C ...

  7. day 31 网络基础的补充

    一.网络基础 1.端口 - 端口,是什么?为什么要有? 端口是为了将同一个电脑上的不同程序进行隔离. IP是找电脑 端口是找电脑上的程序 示例: MySQL是一个软件,软件帮助我们在硬盘上进行文件操作 ...

  8. 关于Go defer的详细使用

    先抛砖引玉defer的延迟调用:defer特性: . 关键字 defer 用于注册延迟调用. . 这些调用直到 return 前才被执.因此,可以用来做资源清理. . 多个defer语句,按先进后出的 ...

  9. Chrom谷歌浏览器没网之最全解决办法之一

    一开始查找百度很多方法都不行,,第一次尝试.最有希望和看着像的是:1.win+r --> 输入regedit 打开注册表2.打开目录HKEY_CURRENT_USER\Software\Micr ...

  10. java 抽象类和接口整理

    java中定义一些不含方法体的方法,方法体的实现交给该类的子类根据自己的具体情况去实现,这样的方法就是abstract修饰的抽象方法,包含抽象方法的类叫抽象类,用abstract修饰 抽象方法: ab ...