当前位置:网站首页># 2156. Find the substring of the given hash value - post order traversal
# 2156. Find the substring of the given hash value - post order traversal
2022-07-04 22:02:00 【Mr Gao】
2156. Find the substring of the given hash value - After the sequence traversal
Given integer p and m , A length of k And subscript from 0 Starting string s The hash value of is calculated according to the following function :
hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m.
among val(s[i]) Express s[i] Subscript in the alphabet , from val(‘a’) = 1 To val(‘z’) = 26 .
Give you a string s And integer power,modulo,k and hashValue . Please return s in first The length is k Of Substring sub , Satisfy hash(sub, power, modulo) == hashValue .
The test data ensure that There is At least one such substring .
Substring Defined as a sequence of consecutive non null characters in a string .
Example 1:
Input :s = “leetcode”, power = 7, modulo = 20, k = 2, hashValue = 0
Output :“ee”
explain :“ee” The hash value of hash(“ee”, 7, 20) = (5 * 1 + 5 * 7) mod 20 = 40 mod 20 = 0 .
“ee” It's a length of 2 The first hash value of is 0 The string of , So we go back to “ee” .
Example 2:
Input :s = “fbxzaad”, power = 31, modulo = 100, k = 3, hashValue = 32
Output :“fbx”
explain :“fbx” The hash value of hash(“fbx”, 31, 100) = (6 * 1 + 2 * 31 + 24 * 312) mod 100 = 23132 mod 100 = 32 .
“bxz” The hash value of hash(“bxz”, 31, 100) = (2 * 1 + 24 * 31 + 26 * 312) mod 100 = 25732 mod 100 = 32 .
“fbx” It's a length of 3 The first hash value of is 32 The string of , So we go back to “fbx” .
Be careful ,“bxz” The hash value of is also 32 , But it is better than... In the string “fbx” Later .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-substring-with-given-hash-value
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The solution code is as follows , This question is for c The language is not very friendly , It takes a lot of work , But take your time , The main idea of solving the problem is to start traversing the tail , Need some mathematical derivation
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;
}
边栏推荐
- Flutter TextField示例
- 玩转gRPC—深入概念与原理
- 做BI开发,为什么一定要熟悉行业和企业业务?
- Redis 排查大 key 的3种方法,优化必备
- gtest从一无所知到熟练使用(2)什么是测试夹具/装置(test fixture)
- ACM Multimedia 2022 | 视觉语言预训练模型中社会偏见的反事实衡量和消除
- 并列图的画法,多排多列
- [advanced C language] array & pointer & array written test questions
- Daily question -leetcode1200- minimum absolute difference - array - sort
- 类方法和类变量的使用
猜你喜欢
Redis 排查大 key 的3种方法,优化必备
How is the entered query SQL statement executed?
gtest从一无所知到熟练使用(3)什么是test suite和test case
湘江鲲鹏加入昇腾万里伙伴计划,与华为续写合作新篇章
From repvgg to mobileone, including mobileone code
从RepVgg到MobileOne,含mobileone的代码
QT—绘制其他问题
MongoDB聚合操作总结
Analysis of maker education technology in the Internet Era
Cloudcompare & open3d DBSCAN clustering (non plug-in)
随机推荐
Spatiotemporal prediction 3-graph transformer
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
Why do you have to be familiar with industry and enterprise business when doing Bi development?
使用 BlocConsumer 同时构建响应式组件和监听状态
Cloudcompare & open3d DBSCAN clustering (non plug-in)
Monitor the shuttle return button
Jerry's ad series MIDI function description [chapter]
How was MP3 born?
Delphi SOAP WebService 服务器端多个 SoapDataModule 实现相同的接口方法,接口继承
类方法和类变量的使用
Flink1.13 SQL basic syntax (I) DDL, DML
解决异步接口慢导致的数据错乱问题
283. 移动零-c与语言辅助数组法
QT—双缓冲绘图
[wechat applet] collaborative work and release
Cadre WebGIS - kalrry
Bizchart+slider to realize grouping histogram
面试官:说说XSS攻击是什么?
Flutter WebView示例
能源势动:电力行业的碳中和该如何实现?