当前位置:网站首页># 2156. 查找给定哈希值的子串-后序遍历
# 2156. 查找给定哈希值的子串-后序遍历
2022-07-04 21:23:00 【Mr Gao】
2156. 查找给定哈希值的子串-后序遍历
给定整数 p 和 m ,一个长度为 k 且下标从 0 开始的字符串 s 的哈希值按照如下函数计算:
hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m.
其中 val(s[i]) 表示 s[i] 在字母表中的下标,从 val(‘a’) = 1 到 val(‘z’) = 26 。
给你一个字符串 s 和整数 power,modulo,k 和 hashValue 。请你返回 s 中 第一个 长度为 k 的 子串 sub ,满足 hash(sub, power, modulo) == hashValue 。
测试数据保证一定 存在 至少一个这样的子串。
子串 定义为一个字符串中连续非空字符组成的序列。
示例 1:
输入:s = “leetcode”, power = 7, modulo = 20, k = 2, hashValue = 0
输出:“ee”
解释:“ee” 的哈希值为 hash(“ee”, 7, 20) = (5 * 1 + 5 * 7) mod 20 = 40 mod 20 = 0 。
“ee” 是长度为 2 的第一个哈希值为 0 的子串,所以我们返回 “ee” 。
示例 2:
输入:s = “fbxzaad”, power = 31, modulo = 100, k = 3, hashValue = 32
输出:“fbx”
解释:“fbx” 的哈希值为 hash(“fbx”, 31, 100) = (6 * 1 + 2 * 31 + 24 * 312) mod 100 = 23132 mod 100 = 32 。
“bxz” 的哈希值为 hash(“bxz”, 31, 100) = (2 * 1 + 24 * 31 + 26 * 312) mod 100 = 25732 mod 100 = 32 。
“fbx” 是长度为 3 的第一个哈希值为 32 的子串,所以我们返回 “fbx” 。
注意,“bxz” 的哈希值也为 32 ,但是它在字符串中比 “fbx” 更晚出现。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-substring-with-given-hash-value
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题代码如下,这题对于c语言不是很友好,做起来很费事,但是大家慢慢来,解题思路主要是尾部开始遍历,需要一些数学推导
long long pend;
int f(char *s,int index,int k,int modulo,int power){
int sum=0;
int i;
long long p=1;
for(i=index;i<=k+index-1;i++){
// printf("%d ||",p);
sum=(sum+(s[i]-'a'+1)*p)%modulo;
if(i==k+index-1){
pend=p;
}
p=(p*power)%modulo;
}
return sum%modulo;
}
char * subStrHash(char * s, int power, int modulo, int k, int hashValue){
long long i;
pend=1;
long long end=f(s,strlen(s)-k,k,modulo,power);
int end_index;
if(end==hashValue){
end_index=strlen(s)-k;
}
// printf("%d ",end);
for(i=strlen(s)-k-1;i>=0;i--){
if(end<pend*(s[i+k]-'a'+1)){
end=end+modulo;
}
long long z=(end-pend*(s[i+k]-'a'+1)%modulo)%modulo;
long long re=(z*power+(s[i]-'a'+1))%modulo;
end=re;
if(re==hashValue){
end_index=i;
}
}
// for(i=0;i<=strlen(s)-k;i++){
// // printf("%d ",i);
// // printf("%d ",f(s,i,k,modulo,power));
// if(f(s,i,k,modulo,power)==hashValue){
// s[i+k]='\0';
// return s+i;
// }
// }
s[end_index+k]='\0';
return s+end_index;
}
边栏推荐
- 一文掌握数仓中auto analyze的使用
- 历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
- 如何借助自动化工具落地DevOps
- Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
- TCP shakes hands three times and waves four times. Do you really understand?
- Jerry's ad series MIDI function description [chapter]
- [weekly translation go] how to code in go series articles are online!!
- File read write
- HDU - 2859 Phalanx(DP)
- MP3是如何诞生的?
猜你喜欢

What is business intelligence (BI), just look at this article is enough

一文掌握数仓中auto analyze的使用

MP3是如何诞生的?

ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start

El tree combined with El table, tree adding and modifying operations

Daily question -leetcode1200- minimum absolute difference - array - sort
![[optimtool.unconstrained] unconstrained optimization toolbox](/img/ef/65379499df205c068ee9bc9df797ac.png)
[optimtool.unconstrained] unconstrained optimization toolbox
![Jerry added the process of turning off the touch module before turning it off [chapter]](/img/28/5e4eb39243a0c973d0b90f76571f9b.png)
Jerry added the process of turning off the touch module before turning it off [chapter]

Redis 排查大 key 的3种方法,优化必备

Three or two things about the actual combat of OMS system
随机推荐
类方法和类变量的使用
解读创客教育中的各类智能化组织发展
[ 每周译Go ] 《How to Code in Go》系列文章上线了!!
淘宝商品评价api接口(item_review-获得淘宝商品评论API接口),天猫商品评论API接口
Kubeadm初始化报错:[ERROR CRI]: container runtime is not running
Bookmark
学习突围3 - 关于精力
Shutter WebView example
redis03——Redis的网络配置与心跳机制
[leetcode] 17. Letter combination of telephone number
SolidWorks工程图添加材料明细表的操作
Compréhension approfondie du symbole [langue C]
【公开课预告】:视频质量评价基础与实践
股票开户流程是什么?使用同花顺手机炒股软件安全吗?
Flutter 返回按钮的监听
flink1.13 sql基础语法(一)DDL、DML
历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
Kubedm initialization error: [error cri]: container runtime is not running
Golang interview finishing three resumes how to write
巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行