当前位置:网站首页># 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;
}
边栏推荐
- 奋斗正当时,城链科技战略峰会广州站圆满召开
- 面试官:说说XSS攻击是什么?
- 如何使用ConcurrentLinkedQueue做一个缓存队列
- Jerry's ad series MIDI function description [chapter]
- Kubedm initialization error: [error cri]: container runtime is not running
- How to remove the black dot in front of the title in word document
- 超详细教程,一文入门Istio架构原理及实战应用
- 解析steam教育中蕴含的众创空间
- GTEST from ignorance to proficiency (4) how to write unit tests with GTEST
- 股票开户佣金最低多少,炒股开户佣金最低网上开户安全吗
猜你喜欢
Redis has three methods for checking big keys, which are necessary for optimization
Daily question-leetcode556-next larger element iii-string-double pointer-next_ permutation
[ 每周译Go ] 《How to Code in Go》系列文章上线了!!
Application practice | Shuhai supply chain construction of data center based on Apache Doris
Compréhension approfondie du symbole [langue C]
LambdaQueryWrapper用法
QT—双缓冲绘图
Redis03 - network configuration and heartbeat mechanism of redis
Word文档中标题前面的黑点如何去掉
创客思维在高等教育中的启迪作用
随机推荐
【C語言】符號的深度理解
【LeetCode】17、电话号码的字母组合
Daily question-leetcode556-next larger element iii-string-double pointer-next_ permutation
For MySQL= No data equal to null can be found. Solution
gtest从一无所知到熟练使用(4)如何用gtest写单元测试
Methods of improving machine vision system
关系型数据库
Jerry's ad series MIDI function description [chapter]
redis03——Redis的网络配置与心跳机制
Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
HDU - 2859 Phalanx(DP)
学习突围3 - 关于精力
Go language loop statement (3 in Lesson 10)
【活动早知道】LiveVideoStack近期活动一览
MYSQL 用!=查询不出等于null的数据,解决办法
Jerry added the process of turning off the touch module before turning it off [chapter]
Why does invariant mode improve performance
历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
Three or two things about the actual combat of OMS system
Shutter WebView example