F - Isomorphic Strings

思路:
字符串hash

对于每一个字母单独hash

对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射

代码:

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 2e5 + ;
const int MOD = 1e9 + ;
const int base = ;
char s[N];
LL Hash[N][], p[N], t[], tt[];
LL get_H(int l, int r, int i) {
return ((Hash[r][i] - Hash[l-][i]*p[r-l+])%MOD + MOD)%MOD;
}
bool check(int x, int y, int len) {
for (int i = ; i < ; i++) {
t[i] = get_H(x, x+len-, i);
tt[i] = get_H(y, y+len-, i);
}
sort(t, t+);
sort(tt, tt+);
for (int i = ; i < ; i++) if(t[i] != tt[i]) return false;
return true;
}
int main() {
int n, m, x, y, len;
scanf("%d%d", &n, &m);
scanf("%s", s+);
p[] = ;
for (int i = ; i <= n; i++) p[i] = (p[i-] * base) % MOD;
for (int i = ; i <= n; i++) {
for (int j = ; j < ; j++) {
Hash[i][j] = (Hash[i-][j] * base + (s[i] == 'a'+j))%MOD;
}
}
while(m--) {
scanf("%d%d%d", &x, &y, &len);
if(check(x, y, len)) puts("YES");
else puts("NO");
}
return ;
}

Codeforces 985 F - Isomorphic Strings的更多相关文章

  1. 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)

    题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...

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

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

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

  4. CodeForces985F -- Isomorphic Strings

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

  5. [LeetCode] Isomorphic Strings

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

  6. leetcode:Isomorphic Strings

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

  7. Codeforces 385B Bear and Strings

    题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...

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

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

  9. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

随机推荐

  1. Java排序之升序与降序

    以前在学校学排序的时候,总是自己写排序的代码,真正到工作中,直接使用java提供的排序方法,但最近发现行业默认的和学习时有些不一样. 以前总是在进行排序时如果前边的数字和后边数字的差为负则交换两个数字 ...

  2. markdown 换行

    基本语法 basic grammar line break

  3. update与select关联执行效率问题

    UPDATE fl_user_space u SET u.`course_count` = (SELECT COUNT(*) FROM fl_course c WHERE c.uid = u.uid) ...

  4. opencv学习之路(6)、鼠标截图,滑动条播放视频

    一.鼠标截图 #include<opencv2/opencv.hpp> #include<iostream> using namespace cv; using namespa ...

  5. PyCharm笔记之配色方案和取消波浪线

    转载:http://blog.csdn.net/xiemanr/article/details/72583718 转载:http://www.jb51.net/article/50689.htm 一. ...

  6. clnt_create: RPC: Program not registered

    原因:[root@nfs nfs]# systemctl start nfs-utils 解决方法:[root@nfs nfs]# systemctl start nfs

  7. MongoDB 创建索引及其他

    索引 以提升查询速度 测试:插入十万条数据到数据库中 for(i=0;i<100000;i++){db.t255.insert({name:'test'+i,age:i})} db.t1.fin ...

  8. GCD与莫比乌斯反演的勾当

    目录 机房最后一个学懵逼钨丝的人 题目一 题目 bzoj1101 机房最后一个学懵逼钨丝的人 题目一 链接 题目没找到 求\(\sum_{1}^{n}\sum_{1}^{m}gcd(i,j)\) 式子 ...

  9. SpringBoot 全局统一记录日志

    1.记录日志 使用aop来记录controller中的请求返回日志 pom.xml引入: <dependency> <groupId>org.springframework.b ...

  10. HDU 4366 Successor(dfs序 + 分块)题解

    题意:每个人都有一个上司,每个人都有能力值和忠诚值,0是老板,现在给出m个询问,每次询问给出一个x,要求你找到x的所有直系和非直系下属中能力比他高的最忠诚的人是谁 思路:因为树上查询很麻烦,所以我们直 ...