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:
- 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.
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的更多相关文章
-
Educational Codeforces Round 44 (Rated for Div. 2) F - Isomorphic Strings
F - Isomorphic Strings 题目大意:给你一个长度为n 由小写字母组成的字符串,有m个询问, 每个询问给你两个区间, 问你xi,yi能不能形成映射关系. 思路:这个题意好难懂啊... ...
-
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 ...
-
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ...
-
Educational Codeforces Round 44 (Rated for Div. 2)
题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...
-
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)的个数,那么实 ...
-
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\),可加 ...
-
Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges
http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...
-
Educational Codeforces Round 41 (Rated for Div. 2)F. k-substrings
题意比较麻烦略 题解:枚举前缀的中点,二分最远能扩展的地方,lcp来check,然后线段树维护每个点最远被覆盖的地方,然后查询线段树即可 //#pragma GCC optimize(2) //#pr ...
-
Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)
题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...
随机推荐
-
修改WAMP中mysql默认空密码
WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按回车 ...
-
iOS 证书调试的理解(Personal)
证书签名 证书:众所周知,我们申请一个Certificate之前,需要先申请一个Certificate Signing Request (CSR) 文件,而这个过程中实际上是生成了一对公钥和私钥,保存 ...
-
Splay tree
类别:二叉排序树 空间效率:O(n) 时间效率:O(log n)内完成插入.查找.删除操作 创造者:Daniel Sleator和Robert Tarjan 优点:每次查询会调整树的结构,使被查询频率 ...
-
国内maven镜像
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> & ...
-
查询死锁和处理死锁(SqlServer)
-------------------查询死锁,极其引起的原因-------------------------------use master go create procedure sp_who_ ...
-
mysql——获取所有table名和table字段名。
获取database所有table名: (参考:http://*.com/questions/2780284/how-to-get-all-table-names-from-a ...
-
selenium2入门 用Yaml文件进行元素管理 (五)
比如界面有一个按钮,id号是test.如果进行对象化的话,就是test.click就可以了.不用每次都要去创建test对象.如果id号变了,我们也只需要改一下test的名称就行了. 使用Yaml需要用 ...
-
C#字符串和数组互转
string str = "a,b,c,d,e"; string[] strArray = str.Split(','); //字符串转数组 ...
-
关于启动过程及log
1.tomcat的启动过程及log 2.webapp的启动过程及log 3.spring的启动过程及log 4.springmvc的启动过程及log 5.web.xml的启动过程及log
-
如何在servlet中获取spring创建的bean
package com.yxf.controller; import java.io.IOException; import javax.servlet.ServletException; impor ...