CodeForces985F:Isomorphic Strings (字符串&hash)
题意:取出字符串Str里的两个串S,T,问对应位置的的字符在否有一一映射关系。
hash:对于每个字符s=‘a’-‘z’,我们任意找一个i,满足Si==s,(代码里用lower_bound在区间找到最小的位置i)其对应的字符为Ti==t。然后我们判断这段区间里s的hash值是否等于t的hash值。不难证明26个字母都满足时对应hash相同时,才有一一映射关系。(但是不明白我的自然溢出的hash版本为什么错了,但是mod(1e9+7)的版本对了?
#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
const int maxn=;
const int seed=;
const int Mod=1e9+;
ll Hash[][maxn],g[maxn];
int a[][maxn],N;
char c[maxn];
ll gethash(int chr,int x,int Len)
{
return ((Hash[chr][x+Len-]-Hash[chr][x-]*g[Len]%Mod)+Mod)%Mod;
}
bool check(int x,int y,int Len)
{
int i,pos;
for(i=;i<;i++){
pos=lower_bound(a[i]+,a[i]++a[i][],x)-a[i];
if(pos>a[i][]||a[i][pos]>x+Len-) continue;
if(gethash(i,x,Len)!=gethash(c[y-x+a[i][pos]]-'a',y,Len)) return false;
}
return true;
}
int main()
{
int Q,x,y,len,i,j;
scanf("%d%d",&N,&Q);
scanf("%s",c+); g[]=;
for(i=;i<=N;i++){
g[i]=g[i-]*seed%Mod;
for(j=;j<;j++)
Hash[j][i]=(Hash[j][i-]*seed+(c[i]-'a'==j))%Mod;
a[c[i]-'a'][++a[c[i]-'a'][]]=i;
}
while(Q--){
scanf("%d%d%d",&x,&y,&len);
if(check(x,y,len)) printf("YES\n");
else printf("NO\n");
}
return ;
}
下面是自然溢出的has版本
#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
const int maxn=;
const int seed=;
ll Hash[][maxn],g[maxn];
int a[][maxn],N;
char c[maxn];
ll gethash(int chr,int x,int Len)
{
return Hash[chr][x+Len-]-Hash[chr][x-]*g[Len];
}
bool check(int x,int y,int Len)
{
int i,pos;
for(i=;i<;i++){
pos=lower_bound(a[i]+,a[i]++a[i][],x)-a[i];
if(pos>a[i][]||a[i][pos]>x+Len-) continue;
if(gethash(i,x,Len)!=gethash(c[y-x+a[i][pos]]-'a',y,Len)) return false;
}
return true;
}
int main()
{
int Q,x,y,len,i,j;
scanf("%d%d",&N,&Q);
scanf("%s",c+); g[]=;
for(i=;i<=N;i++){
g[i]=g[i-]*seed;
for(j=;j<;j++)
Hash[j][i]=Hash[j][i-]*seed+(c[i]-'a'==j);
a[c[i]-'a'][++a[c[i]-'a'][]]=i;
}
while(Q--){
scanf("%d%d%d",&x,&y,&len);
if(check(x,y,len)) printf("YES\n");
else printf("NO\n");
}
return ;
}
CodeForces985F:Isomorphic Strings (字符串&hash)的更多相关文章
- CF985F Isomorphic Strings (字符串Hash,巧解)
题目链接 题意翻译 给你一个长度为 \(n\) 的字符串,\(m\) 次询问. 问两个相同长度的子串是否匹配. 我们称两个子串是匹配的,当且仅当其满足: 其中一个子串的字母可替代另一个子串的字母 例如 ...
- CodeForces985F -- Isomorphic Strings
F. Isomorphic Strings time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- Leetcode 205 Isomorphic Strings 字符串处理
判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...
- 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)
题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- 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能不能形成映射关系. 思路:这个题意好难懂啊... ...
- [LeetCode] Isomorphic Strings
Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...
- LA4671 K-neighbor substrings(FFT + 字符串Hash)
题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...
随机推荐
- hdu3987,最小割时求最少割边数
题意:求最小割时候割边最少的数量.算法:先求dinic一遍,跑出残网络,再把该网络中满流量(残量为0)的边 残量改为1,其他边残量改为无穷,则再跑一次最大流,所得即为答案.(思,最小割有喝多组,但是要 ...
- Perl、PHP、Python、Java和Ruby的比较
提问 ◆ Perl.Python.Ruby和PHP各自有何特点? ◆ 为什么动态语言多作为轻量级的解决方案? ◆ LAMP为什么受欢迎? ◆ Ruby on Rails为什么会流行? ◆ 编程语言的发 ...
- LeetCode第一题以及时间复杂度的计算
问题描述:给定一组指定整数数组,找出数组中加和等于特定数的两个数. 函数(方法)twoSum返回这两个数的索引,index1必须小于index2. 另外:你可以假设一个数组只有一组解. 一个栗子: I ...
- Apache Beam 传 大数据杂谈
1月10日,Apache软件基金会宣布,Apache Beam成功孵化,成为该基金会的一个新的顶级项目,基于Apache V2许可证开源. 2003年,谷歌发布了著名的大数据三篇论文,史称三驾马车:G ...
- Go -- PipleLine
1.pipeline的产生 从一个现象说起,有一家咖啡吧生意特别好,每天来的客人络绎不绝,客人A来到柜台,客人B紧随其后,客人C排在客人B后面,客人D排在客人C后面,客人E排在客人D后面,一直排到店面 ...
- HDU 1501
Zipper Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 性能測试JMeter趟的坑之JMeter的bug:TPS周期性波动问题
先说下问题: 我在做性能測试时,使用JMeter搞了100个并发,以100TPS的压力压測十分钟,但压力一直出现波动.并且出现波动时JMeter十分卡,例如以下图: 周期性TPS波动 各种猜測: 所以 ...
- mysql语法、特殊符号及正則表達式的使用
http://blog.csdn.net/pipisorry/article/details/46773545 sql语言 结构化的查询语言.(Structured Query Language),是 ...
- Android版DesiredCapabilities参数配置
前言 每一个App测试都应指定是在什么平台下,那个设备中运行那个App,而在Appium中主要是通过DesiredCapabilities来配置的. DesiredCapabilities的作用,负责 ...
- REST RPC HTTP vs 高性能二进制协议 序列化和通信协议
edisonchou https://mp.weixin.qq.com/s/-XZXqXawR-NxJMPCeiNsmg .NET Core微服务之服务间的调用方式(REST and RPC) Edi ...