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 4abacaba1 1 11 4 22 1 32 4 3
YESYESNOYES
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;
}
转载于:https://www.cnblogs.com/JYYHH/p/9071465.html
CodeForces - 985F Isomorphic Strings相关推荐
- CodeForces - 985F Isomorphic Strings(字符串哈希)
题目链接:点击查看 题目大意:首先规定同构字符串,若字符串s和字符串t互为同构字符串,则必须满足: 两个字符串长度相同 s中的字符种类数与t中的字符种类数相同 s中的每一个字母在t中都有对应,且必须是 ...
- leetcode之Isomorphic strings
问题来源:Isomorphic strings 问题描述:给定两个字符串,判断它俩是否满足同构关系.同构关系表示两个字符串的字符能一一对应,例如title和paper构成同构关系,而foo和bar不构 ...
- hdu 6863 Isomorphic Strings
Isomorphic Strings 题解:暴力枚举n的所有因子,判断是否合法.判断合法第一步:判断每段中各字符出现次数是否相同,第二部:判断每段的最小表示是否相同.因子还可以减枝. #include ...
- codeforces 616F Expensive Strings
codeforces 616F Expensive Strings (广义后缀自动机) https://codeforces.com/contest/616/problem/F 题意: 给你n个字符串 ...
- 205 Isomorphic Strings
205 Isomorphic Strings 链接:https://leetcode.com/problems/isomorphic-strings/ 问题描述: Given two strings ...
- LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
翻译 给定两个字符串s和t,决定它们是否是同构的.假设s中的元素被替换能够得到t,那么称这两个字符串是同构的.在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两个字 ...
- [LeetCode] Isomorphic Strings - 字符串操作:数组计数字符个数问题
题目概述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...
- leetcode python3 简单题205. Isomorphic Strings
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零五题 (1)题目 英文: Given two strings s and t ...
最新文章
- MiBioGen联盟倡议:综合分析人类基因组与菌群关系研究
- C# 操作Excel之旁门左道 [ C# | Excel ]
- [C++] stack和queue的常用函数
- 前端ajax封装对象数组,后台的取法
- 推荐20个很有帮助的 Web 前端开发教程
- mysql启动触发器_MYSQL中禁用/启动触发器
- HTML5应用程序网站视差模板下载
- 礼橙专车、青菜拼车今日起改名啦!
- Python稳基修炼的经典案例4(计算机二级、初学者必须掌握的例题)
- SQL关于数据库完整性
- 操作 SQL Server Mobile 2005 数据库的常用 C# 代码 (转自黎波)
- [转载] PYTHON 网络编程
- 聊聊我所从事过的通信行业
- golang xorm cmd xorm工具使用 reverse 反转一个数据库结构,生成代码
- 6.Entity FrameWork Core 5.0 删除、修改数据
- vue移动端获取定位_关于关于vue移动端经纬度定位以及POI检索周边小区
- Postman中文文档——安装与更新
- Kafka:Topic概念与API介绍
- 小宝挪车自研第二代挪车通知技术,保护隐私无死角!
- 光纤跳线的分类和概述
热门文章
- 旧电脑装什么系统最快_旧电脑装什么系统最好?试试Porteus,小巧强悍,直降CPU占用率!...
- spring boot中@ResponseBody等注解的作用与区别
- java调用天气预报api_java调用中国天气网api获得天气预报信息
- 划重点了!网络推广网站在建设时期该如何进行优化?
- 深圳网络推广是如何提高网站的收录比的?
- 企业网络推广期间对于易流失用户群体企业网络推广有话说
- mongodb java findone_java-MongoRepository findOne使用“ id”代替“ _id”
- 裸奔浏览器_大概是最好用的隐私浏览器 - Firefox Focus
- 计算机用户名更改不了,简单几步解决win10电脑用户名改不了的问题
- 易语言录入数据到access里重复提示_一招搞定:excel数据共享协同