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

题目连接:

http://codeforces.com/contest/985/problem/F

Description

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.

Sample Input

7 4
abacaba
1 1 1
1 4 2
2 1 3
2 4 3

Sample Output

YES
YES
NO
YES

题意

判断字符串是否同构

Judging two string have the same structure or not.

题解:

将字符串抽象成26个01串,如果对应字母的26个对应位置的子串相同,则字符串同构。

Create 26 01-string, if the substring which is locate at the same position, two string have the same structure.

代码

#include <bits/stdc++.h>

using namespace std;

int n, k;
string d;
bool a[50][200010];
vector<int> v[26];
long long hs[50][200010];
long long c[200010]; const long long inf = 0x3f3f3f3f;
const long long MOD = 1e9 + 7; int main() {
cin >> n >> k >> d;
d = " " + d;
c[0] = 1;
for (int i = 1; i < d.size(); i++) {
a[d[i] - 'a'][i] = 1;
v[d[i] - 'a'].push_back(i);
c[i] = (c[i - 1] * inf) % MOD;
for (int j = 0; j < 26; j++)
hs[j][i] = (hs[j][i - 1] * inf + a[j][i]) % MOD;
} for (int i = 1; i <= k; i++) {
int x, y, z;
cin >> x >> y >> z;
bool flag = 1;
for (int j = 0; j < 26; j++) {
int pos = lower_bound(v[j].begin(), v[j].end(), x )-v[j].begin();
if (pos<v[j].size())
pos = v[j][pos];
else
continue;
if (pos >= x + z) continue;
int dual = d[pos + y - x]-'a';
long long h1 = ((hs[j][x + z - 1] - hs[j][x - 1] * c[z]) % MOD + MOD) % MOD;
long long h2 = ((hs[dual][y + z - 1] - hs[dual][y - 1] * c[z]) % MOD + MOD) % MOD;
if (h1 != h2) {
flag = 0;
break;
}
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
} }

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings的更多相关文章

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

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

  2. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  3. Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

    Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ​ ...

  4. Educational Codeforces Round 44 (Rated for Div. 2)

    题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...

  5. Educational Codeforces Round 50 (Rated for Div. 2) F - Relatively Prime Powers(数学+容斥)

    题目链接:http://codeforces.com/contest/1036/problem/F 题意: 题解:求在[2,n]中,x != a ^ b(b >= 2 即为gcd)的个数,那么实 ...

  6. Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理

    https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...

  7. Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges

    http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...

  8. Educational Codeforces Round 41 (Rated for Div. 2)F. k-substrings

    题意比较麻烦略 题解:枚举前缀的中点,二分最远能扩展的地方,lcp来check,然后线段树维护每个点最远被覆盖的地方,然后查询线段树即可 //#pragma GCC optimize(2) //#pr ...

  9. Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)

    题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...

随机推荐

  1. JQUERY之表单验证案例

    <!-- 需求: 用户注册页面要有用户名.密码.确认密码.邮箱 用户名文本框:用户名不能为空,且必须为数字与字母的6到12位的组合 密码框:密码不能为空,六到八位数字或字母的组合 确认密码框:确 ...

  2. scrapy 之自定义命令运行所有爬虫文件

    1.在spider文件夹同级目录创建commands python包 2.在包下创建command.py文件 3.从scrapy.commands包下引入ScrapyCommand 4.创建一个类,继 ...

  3. Python设计模式 - UML - 活动图(Activity Diagram)

    简介 活动图描述从一个活动到另一个活动的执行顺序.约束条件.引用对象及状态结果等方面的控制流,适用于对业务用例.工作流程或程序实现建模. 活动图建模步骤 - 确定活动图的范围和边界,对哪些工作流.哪些 ...

  4. Myeclipse加载php插件

    下载PHPEclipse-1.2.3.200910091456PRD-bin.zip 解压缩后.发现内容包含:两个目录features和plugins,一个xml文件site.xml 全部扔进myec ...

  5. Intro to Airplane Physics in Unity 3D – 2017 and 2018

    Info:DescriptionHave you ever wanted to build your own Airplane Physics using the Rigidbody componen ...

  6. Codeforces Round #491 (Div. 2)

    Codeforces Round #491 (Div. 2) https://codeforces.com/contest/991 A #include<bits/stdc++.h> us ...

  7. gerapy 实现自动化部署

    1 安装 2 在需要部署的目录下运行 gerapy init 会在当前目录下生成一个gerapy目录,并在gerapy目录下有一个projects 目录 3 切换到gerapy 目录 cd gerap ...

  8. 记录下本地修改php版本的过程, 本地PHP目录位置,PHP-FPM目录位置

    由于我在Cellar下安装了多个PHP版本,所以这里记录下如何修改本地的PHP版本 cd /usr/local/bin cp php71 php cp php71-fpm php-fpm vscode ...

  9. Find out where to contain the smartforms

    Go to table E071 and give smarforms name and it will give the transport req for that. Run SE03, choo ...

  10. Java 数据类型与运算符

    JAVA数据类型分基本(内置)数据类型和引用数据类型. 区别: 基本数据类型在被创建时,在栈上给其划分一块内存,将数值直接存储在栈上. 引用数据类型在被创建时,首先要在栈上给其引用(句柄)分配一块内存 ...