CF985F Isomorphic Strings
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:
- f(si)=ti f(s_{i})=t_{i} f(si)=ti for any index i i i ,
- for any character there is exactly one character that f(x)=y f(x)=y f(x)=y ,
- 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.
输入输出样例
7 4
abacaba
1 1 1
1 4 2
2 1 3
2 4 3
YES
YES
NO
YES
说明
The queries in the example are following:
- substrings "a" and "a" are isomorphic: f(a)=a f(a)=a f(a)=a ;
- substrings "ab" and "ca" are isomorphic: f(a)=c f(a)=c f(a)=c , f(b)=a f(b)=a f(b)=a ;
- 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;
- 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 .
#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 ;
}
CF985F Isomorphic Strings的更多相关文章
- CF985F Isomorphic Strings (字符串Hash,巧解)
题目链接 题意翻译 给你一个长度为 \(n\) 的字符串,\(m\) 次询问. 问两个相同长度的子串是否匹配. 我们称两个子串是匹配的,当且仅当其满足: 其中一个子串的字母可替代另一个子串的字母 例如 ...
- [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 ...
- 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 ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 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能不能形成映射关系. 思路:这个题意好难懂啊... ...
- 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
随机推荐
- ubuntu各系统双网卡绑定
Ubuntu14.04双网卡绑定 2.1 确定网卡名称 首先确定两块网卡的名称,一般为eth0.eth1,如果有自己添加的网卡名称可能不同,在安装系统的时候可以看到,通过ipmaddr命令可以查看所有 ...
- python核心编程2 第十二章 练习
12–5. 使用 __import__().(a) 使用 __import__ 把一个模块导入到你的名称空间. 你最后使用了什么样的语法? (b) 和上边相同, 使用 __import__() 从指定 ...
- Document .load与Document .ready的区别
页面加载完成有两种事件 1.load是当页面所有资源全部加载完成后(包括DOM文档树,css文件,js文件,图片资源等),执行一个函数 问题:如果图片资源较多,加载时间较长,onload后等待执行的函 ...
- react脚手架搭建1
23:01:17 react脚手架搭建 (个人用的是webstorm,所以分享下webstorm中的创建react脚手架项目的方法) 1.创建新项目(前提是下载nodejs环境) 2.下载好在webs ...
- CSS基础全荟
一.CSS概述 1.css是什么?? 层叠样式表 2.css的引入方式 1.行内样式 在标签上加属性style="属性名1:属性值1;属性名2:属性值2;..." 2.内嵌式 ...
- web开发学习路线
第一阶段: HTML+CSS: HTML进阶.CSS进阶.div+css布局.HTML+css整站开发. JavaScript基础: Js基础教程.js内置对象常用方法.常见DOM树操作大全.ECMA ...
- php 电商系统SKU库存设计
sku 全称为:Stock Keeping Unit,是库存进出计量的基本单元. 我们一般会在电商网站基本都会看到 比如淘宝,JD 淘宝和JD的 方式可能不一样,因为我不清楚他们具体是如何设计的, J ...
- x-pack本地安装方式
一.首先下载本地安装包,我使用的ELK是5.6.1版本: https://artifacts.elastic.co/downloads 二.进入到elasticsearch/bin(所有节点)和kib ...
- JavaSE 第二次学习随笔(五)
/* * 中文乱码出现的情况研究 * 注意点:乱码解决的办法是再编码再解码 * 但是如果是编码出错了,无法解决.如果是解码出错了,可以利用再编码再解码 * * * 编码 解码 结果 * GBK utf ...
- hadoop生态搭建(3节点)-05.mysql配置_单节点
# ==================================================================node1 # ======================== ...