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 ...
随机推荐
- HTML网页滚动加载--mark一下
console控制台: >: function stroll(){ window.scrollTo(, document.body.scrollHeight); }; >: window. ...
- npm start 修改启动端口的不同方式
antd的启动配置文件基于package.json文件,配合roadhog使用时,启动配置是: "scripts": { "start": "road ...
- 树莓派学习笔记——I2C设备载入和速率设置
原文:http://blog.csdn.net/xukai871105/article/details/18234075 1.载入设备 方法1——临时载入设备 sudo modprobe -r i2c ...
- XSS学习分支图
转载请注明出处:http://blog.csdn.net/cym492224103 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2 ...
- C语言之基本算法34—分解质因数(方法一)
//矩阵基础 /* ================================================================== 题目:输入一个正整数.将其分解为质因式,如:6 ...
- C#语法复习2
第五章 方法 1.方法是一块具有名称的代码 包括:方法体.方法头 局部变量必须被赋值才可以执行下面的操作.实例变量有隐式初始化.有时候,类型推断可以用var关键字,类似于C++当中的auto.用于局部 ...
- C++经典面试题解析
1. // BlankTest.cpp : 定义控制台应用程序的入口点. //题目:将一个文件中的一组整数排序后输出到另一个文件中 #include "stdafx.h" #inc ...
- YII 多子域名同步登录
a.meylou.com和b.meylou.com不做登录.c.meylou.com这个专门做用户登录.c站登录之后a,b站点同时登录. 第一步:修改php.ini配置文件,把cookie_domai ...
- 阿里云 ubuntu 14.04 模板上安装 docker
ubuntu 14.04 的内核是 3.13 ,所以内核不用升级. 安装过程例如以下: # apt-get update # apt-get install apt-transport-https # ...
- postgres启动过程分析
今天来学习一下pg启动的过程. pg的启动命令./bin/postgres -D path/to/data. 1.主体监控进程 postmaster进程进入无限循环,等待客户端请求并为之提供请求的服务 ...