题意:取出字符串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. Compose

    安装与卸载 Compose 支持 Linux.macOS.Windows 10 三大平台. Compose 可以通过 Python 的包管理工具 pip 进行安装,也可以直接下载编译好的二进制文件使用 ...

  2. HDU 1358字符串循环节问题 ,next数组

    求字符串循环节,要求每前i个字符串前缀是否循环,有的话打印出来. 我对j=next[i]数组(未优化,从0开始,第一个为-1,)理解:字符s[i]的前面的字符串,最长的相同的前缀和后缀 的长度,因此, ...

  3. I.Tower Defense

    给你p个重塔,q个轻塔,把这些塔放在n*m的图中,这些塔会相互攻击同行同列的,轻塔不能受到攻击,重塔能承受一个塔的攻击, 问放的方法数. 先假定n < m. 可以先枚举放轻塔的个数为s,显然,方 ...

  4. [Bzoj3676][Apio2014]回文串(后缀自动机)(parent树)(倍增)

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 3396  Solved: 1568[Submit][Statu ...

  5. Linux配置防火墙添加端口(Ubuntu/Debian无法使用此方法)

    注意:Ubuntu/Debian无法使用此方法 1.打开iptables vi /etc/sysconfig/iptables 2.添加防火墙规则 规则参考:http://www.cnblogs.co ...

  6. How to fill the background with image in landscape in IOS? 如何使image水平铺满屏幕

    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:self.view.frame];    [backgroundIm ...

  7. 高级算法设计讲义 Lecture Notes for Advanced Algorithm Design

    (Last modification: 2012-12-17) Textbooks: (1) David Williamson, David Shmoys. The Design of Approxi ...

  8. Java面试题集(151-180)

    摘要:这部分包括了Spring.Spring MVC以及Spring和其它框架整合以及測试相关的内容,除此之外还包括了大型站点技术架构相关面试内容. 151. Spring中的BeanFactory和 ...

  9. 一种排序(nyoj8)(简单排序)

    一种排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 如今有非常多长方形.每个长方形都有一个编号,这个编号能够反复.还知道这个长方形的宽和长,编号.长.宽都是整数 ...

  10. 如何动态地给vSphere虚拟机模板注入信息

    在做vSphere自动化安装过程中,遇到这样一个需求:将vCenter Server做成模板,在给用户自动化装好vSphere后, 下载vCenter Server模板并启动虚拟机,然后将vCente ...