题意:取出字符串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)的更多相关文章

  1. CF985F Isomorphic Strings (字符串Hash,巧解)

    题目链接 题意翻译 给你一个长度为 \(n\) 的字符串,\(m\) 次询问. 问两个相同长度的子串是否匹配. 我们称两个子串是匹配的,当且仅当其满足: 其中一个子串的字母可替代另一个子串的字母 例如 ...

  2. CodeForces985F -- Isomorphic Strings

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

  3. Leetcode 205 Isomorphic Strings 字符串处理

    判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...

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

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

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

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

  6. LeetCode 205. 同构字符串(Isomorphic Strings)

    205. 同构字符串 205. Isomorphic Strings

  7. Codeforces 985 F - Isomorphic Strings

    F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...

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

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

  9. [LeetCode] Isomorphic Strings

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

  10. LA4671 K-neighbor substrings(FFT + 字符串Hash)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...

随机推荐

  1. docker-清理none镜像等操作

    有时候由于调试代码产生很多的none的image,挨个清理会有些麻烦,从网上找到的命令 // 停止所有容器 docker ps -a | grep "Exited" | awk ' ...

  2. Java 并发编程中使用 ReentrantLock 替代 synchronized 关键字原语

    Java 5 引入的 Concurrent 并发库软件包中,提供了 ReentrantLock 可重入同步锁,用来替代 synchronized 关键字原语,并可提供更好的性能,以及更强大的功能.使用 ...

  3. Wannafly挑战赛11 D 题 字符串hash + 卡常

    题目链接 https://ac.nowcoder.com/acm/contest/73#question map与order_map https://blog.csdn.net/BillCYJ/art ...

  4. 带您了解Oracle层次查询

    http://database.51cto.com/art/201010/231539.htm Oracle层次查询(connect by )是结构化查询中用到的,下面就为您介绍Oracle层次查询的 ...

  5. [转载]CodeIgniter配置之URL

    应该有很多项目中会有这样的情况,通过 http://pc.local 可以访问,若通过 http://localhost/pc/public 则会出现一些图片.样式显示不到,超链接出错的情况,项目的移 ...

  6. Codeforces Round #262 (Div. 2)460A. Vasya and Socks(简单数学题)

    题目链接:http://codeforces.com/contest/460/problem/A A. Vasya and Socks time limit per test 1 second mem ...

  7. VC++ 学习笔记(四):停止还是暂停这个系列

    我已经很久没有更新这个话题了,原因是多方面的,比如比较忙,比如我参与的项目不使用C++.最近因为需要在C#的客户端中调用第三方的C++API,又想起了这个话题.在跟公司里的C++方面专家聊过之后,我有 ...

  8. 机器学习经典算法具体解释及Python实现--K近邻(KNN)算法

    (一)KNN依旧是一种监督学习算法 KNN(K Nearest Neighbors,K近邻 )算法是机器学习全部算法中理论最简单.最好理解的.KNN是一种基于实例的学习,通过计算新数据与训练数据特征值 ...

  9. PCH in Xcode 6

    本文转载至 http://blog.csdn.net/wbdwsqwwn/article/details/40476151   新建文件 ⌘+N 选择 iOS/Mac -> Other -> ...

  10. liberOJ #6173. Samjia 和矩阵 hash+后缀数组

    #6173. Samjia 和矩阵 题目链接  : 点这里 题目描述 给你一个只包含大写字母的矩阵,求有多少本质不同的子矩阵. 输入格式 第一行包含两个整数 nnn , mmm ,表示矩阵 nnn 行 ...