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 ...
随机推荐
- 在 VirtualBox 5.0 系列中让虚拟机支持 USB 3.0 必须开启 APIC
VirtualBox 5.0 系列正式支持 USB 3.0,能够在宿主机支持 USB 3.0 的情况下,让虚拟机也选择具备 USB 3.0 的功能.但是经过多方试验,发现必须在 VirtualBox ...
- AC日记——草地排水 codevs 1993
1993 草地排水 USACO 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 在农夫约翰的农场上,每 ...
- AC日记——友好城市 洛谷 P2782
题目背景 无 题目描述 有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市.北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同.没对友好城市都向政府申请在河上 ...
- Eclipse、IDEA安装JavaFX
简介 JavaFX插件 e(fx)eclipse是一组工具和必要的库,它们帮助您执行JavaFX编程,确保您已经作为插件将其安装在eclipse上了. JavaFX Scene Builder Jav ...
- Java中的网络基础
先来一张图记录一下大概思路,之后再更新具体的代码实现.基本上来说,前半部分自己会编写一个基于socket编程的多客户端dos聊天服务器,后半部分可以实现与已有的一些服务器(比如www.google.c ...
- hadoop学习 的yarn
Yarn的产生 mapReduc1.0 1单点故障 2扩展效率低 3资源利用率高 降低运维成本 方便数据共享 多计算框架支持 MapReduce Spark Storm Yarn的架构图 Yarn模块 ...
- Oracle EBS - 工单状态
Job status update 1. Job的几种状态 unreleased --未核发 released--已核发 complete --完成 complete no charges--完成不计 ...
- Js学习第十天----函数
函数 什么是函数?函数是由事件驱动的或者当他被调用时运行的可反复使用代码块.预计没明确,个人觉得函数就是能完毕一个功能的代码块. 看个案例: <!DOCTYPE html> <htm ...
- hdu3076ssworld VS DDD 概率dp
//ssworld VS DDD 两个人有血量值 hp1 , hp2 //两人掷骰子得到每一点的概率已知 //ssword赢的概率 //dp[i][j] 表示有第一个人血量为i.第二个人的血量为j ...
- java开始到熟悉66-69
本次内容:DateFormat类 1.DateFormat类 package array; /** * 时间和字符串之间的转化 */ import java.text.DateFormat; impo ...