当前位置:网站首页># 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;
}
边栏推荐
- 广电五舟与华为签署合作协议,共同推进昇腾AI产业持续发展
- 一文掌握数仓中auto analyze的使用
- Compréhension approfondie du symbole [langue C]
- What is the stock account opening process? Is it safe to use flush mobile stock trading software?
- Jerry's ad series MIDI function description [chapter]
- Flutter WebView示例
- Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
- 从RepVgg到MobileOne,含mobileone的代码
- How was MP3 born?
- Analyzing the maker space contained in steam Education
猜你喜欢

QT - double buffer plot

机器学习笔记 - 互信息Mutual Information

Shutter textfield example

案例分享|金融业数据运营运维一体化建设

如何借助自动化工具落地DevOps

Sorting and sharing of selected papers, systems and applications related to the most comprehensive mixed expert (MOE) model in history

Analyzing the maker space contained in steam Education

使用 BlocConsumer 同时构建响应式组件和监听状态

bizchart+slider实现分组柱状图

输入的查询SQL语句,是如何执行的?
随机推荐
Flutter 返回按钮的监听
Lambdaquerywrapper usage
[optimtool.unconstrained] unconstrained optimization toolbox
Case sharing | integrated construction of data operation and maintenance in the financial industry
Super detailed tutorial, an introduction to istio Architecture Principle and practical application
一文掌握数仓中auto analyze的使用
[leetcode] 17. Letter combination of telephone number
置信区间的画法
开户哪家券商比较好?网上开户安全吗
关系型数据库
What is business intelligence (BI), just look at this article is enough
从RepVgg到MobileOne,含mobileone的代码
bizchart+slider实现分组柱状图
Is it safe to open an account in the stock of Caicai college? Can you only open an account by digging money?
gtest从一无所知到熟练使用(4)如何用gtest写单元测试
MongoDB中的索引操作总结
Cloudcompare & open3d DBSCAN clustering (non plug-in)
new IntersectionObserver 使用笔记
输入的查询SQL语句,是如何执行的?
Nat. Commun.| 机器学习对可突变的治疗性抗体的亲和力和特异性进行共同优化