做h5的网站页面设计,临海制作网站公司,直播做网站,网上书城网站开发的数据字典本文涉及知识点
滚动哈希 二分查找算法合集
LeetCode 1044. 最长重复子串
给你一个字符串 s #xff0c;考虑其所有 重复子串 #xff1a;即 s 的#xff08;连续#xff09;子串#xff0c;在 s 中出现 2 次或更多次。这些出现之间可能存在重叠。 返回 任意一个 可能具…本文涉及知识点
滚动哈希 二分查找算法合集
LeetCode 1044. 最长重复子串
给你一个字符串 s 考虑其所有 重复子串 即 s 的连续子串在 s 中出现 2 次或更多次。这些出现之间可能存在重叠。 返回 任意一个 可能具有最长长度的重复子串。如果 s 不含重复子串那么答案为 “” 。 示例 1 输入s “banana” 输出“ana” 示例 2 输入s “abcd” 输出“” 提示 2 s.length 3 * 104 s 由小写英文字母组成
二分查找滚动哈希
令 Check(len) 返回 是否存在长度为len的重复字符串 len1 len2如果Check(len2)为true则Check(len1)一定为true 即 len ∈ \in ∈ [0,len3]为Check(len)为truelen ∈ \in ∈ [len31,n] Check(len)为false。 寻找最后一个true故用左闭右开空间。
Check函数
len 0 为0返回true。 用滚动函数计算 s[i…ilen-1]的哈希值 i len s.length 并将哈希值记录到set中如果存在重复值返回true。
时间复杂度O(nlogn) 二分查找O(logn) Check函数O(n)
代码
核心代码
templateint MOD 1000000007
class C1097Int
{
public:C1097Int(long long llData 0) :m_iData(llData% MOD){}C1097Int operator(const C1097Int o)const{return C1097Int(((long long)m_iData o.m_iData) % MOD);}C1097Int operator(const C1097Int o){m_iData ((long long)m_iData o.m_iData) % MOD;return *this;}C1097Int operator-(const C1097Int o){m_iData (m_iData MOD - o.m_iData) % MOD;return *this;}C1097Int operator-(const C1097Int o){return C1097Int((m_iData MOD - o.m_iData) % MOD);}C1097Int operator*(const C1097Int o)const{return((long long)m_iData * o.m_iData) % MOD;}C1097Int operator*(const C1097Int o){m_iData ((long long)m_iData * o.m_iData) % MOD;return *this;}C1097Int operator/(const C1097Int o)const{return *this * o.PowNegative1();}C1097Int operator/(const C1097Int o){*this / o.PowNegative1();return *this;}bool operator(const C1097Int o)const{return m_iData o.m_iData;}bool operator(const C1097Int o)const{return m_iData o.m_iData;}C1097Int pow(long long n)const{C1097Int iRet 1, iCur *this;while (n){if (n 1){iRet * iCur;}iCur * iCur;n 1;}return iRet;}C1097Int PowNegative1()const{return pow(MOD - 2);}int ToInt()const{return m_iData;}
private:int m_iData 0;;
};//iCodeNum 必须大于等于可能的字符数
templateint MOD 1000000007
class CHashStr {
public:CHashStr(string s, int iCodeNum, int iCodeBegin 1, char chBegin a) {m_c s.length();m_vP.resize(m_c 1);m_vP[0] 1;m_vHash.resize(m_c 1);for (int i 0; i m_c; i){const int P iCodeBegin iCodeNum;m_vHash[i 1] m_vHash[i] * P s[i] - chBegin iCodeBegin;m_vP[i 1] m_vP[i] * P;}}//iMinValue将被编码为0,iMaxValue被编码为iMaxValue-iMinValue。CHashStr(const int* data, int len, int iMinValue 0, int iMaxValue 9) {m_c len;m_vP.resize(m_c 1);m_vP[0] 1;m_vHash.resize(m_c 1);const int P iMaxValue - iMinValue 1;for (int i 0; i m_c; i){const int iCurCode data[i] - iMinValue;assert((iCurCode 0) (iCurCode P));m_vHash[i 1] m_vHash[i] * P iCurCode;m_vP[i 1] m_vP[i] * P;}}//包括left rightint GetHash(int left, int right){return (m_vHash[right 1] - m_vHash[left] * m_vP[right - left 1]).ToInt();}inline int GetHash(int right){return m_vHash[right 1].ToInt();}int GetHashExincludeRight(int left, int right){return (m_vHash[right] - m_vHash[left] * m_vP[right - left]).ToInt();}inline int GetHashExincludeRight(int right){return m_vHash[right].ToInt();}int m_c;vectorC1097IntMOD m_vP;vectorC1097IntMOD m_vHash;
};templateint MOD2 1000000009
class C2HashStr
{
public:C2HashStr(string s) {m_pHash1 std::make_uniqueCHashStr(s, 26);m_pHash2 std::make_unique CHashStrMOD2(s, 27, 0);}C2HashStr(const int* data, int len, int iMinValue 0, int iMaxValue 9){m_pHash1 std::make_uniqueCHashStr(data, len, iMinValue, iMaxValue);m_pHash2 std::make_unique CHashStrMOD2(data, len, iMinValue, iMaxValue);}//包括left rightlong long GetHash(int left, int right){return (long long)m_pHash1-GetHash(left, right) * (MOD2 1) m_pHash2-GetHash(left, right);}long long GetHash(int right){return (long long)m_pHash1-GetHash(right) * (MOD2 1) m_pHash2-GetHash(right);}//包括Left不包括Rightlong long GetHashExincludeRight(int left, int right){return (long long)m_pHash1-GetHashExincludeRight(left, right) * (MOD2 1) m_pHash2-GetHashExincludeRight(left, right);}long long GetHashExincludeRight(int right){return (long long)m_pHash1-GetHashExincludeRight(right) * (MOD2 1) m_pHash2-GetHashExincludeRight(right);}
private:std::unique_ptrCHashStr m_pHash1;std::unique_ptrCHashStrMOD2 m_pHash2;
};namespace NBinarySearch
{templateclass INDEX_TYPE, class _PrINDEX_TYPE FindFrist(INDEX_TYPE left, INDEX_TYPE rightInclue, _Pr pr){while (rightInclue - left 1){const auto mid left (rightInclue - left) / 2;if (pr(mid)){rightInclue mid;}else{left mid;}}return rightInclue;}templateclass INDEX_TYPE, class _PrINDEX_TYPE FindEnd(INDEX_TYPE leftInclude, INDEX_TYPE right, _Pr pr){while (right - leftInclude 1){const auto mid leftInclude (right - leftInclude) / 2;if (pr(mid)){leftInclude mid;}else{right mid;}}return leftInclude;}
}class Solution {
public:string longestDupSubstring(string s) {string ret;C2HashStr dh(s);auto Check [](int len) {if (0 len) { ret ; return true; }unordered_setlong long setHas;for (int i 0; i len s.length(); i) {auto cur dh.GetHashExincludeRight(i, i len);if (setHas.count(cur)) {ret s.substr(i, len);return true;}setHas.emplace(cur);}return false;};NBinarySearch::FindEnd(0, (int)s.length() 1, Check);return ret;}
};单元测试
templateclass T1,class T2
void AssertEx(const T1 t1, const T2 t2)
{Assert::AreEqual(t1 , t2);
}templateclass T
void AssertEx(const vectorT v1, const vectorT v2)
{Assert::AreEqual(v1.size(), v2.size()); for (int i 0; i v1.size(); i){Assert::AreEqual(v1[i], v2[i]);}
}templateclass T
void AssertV2(vectorvectorT vv1, vectorvectorT vv2)
{sort(vv1.begin(), vv1.end());sort(vv2.begin(), vv2.end());Assert::AreEqual(vv1.size(), vv2.size());for (int i 0; i vv1.size(); i){AssertEx(vv1[i], vv2[i]);}
}namespace UnitTest
{string s;TEST_CLASS(UnitTest){public:TEST_METHOD(TestMethod1){s banana;auto res Solution().longestDupSubstring(s);AssertEx(string(ana), res);}TEST_METHOD(TestMethod2){s abcd;auto res Solution().longestDupSubstring(s);AssertEx(string(), res);}TEST_METHOD(TestMethod3){s aa;auto res Solution().longestDupSubstring(s);AssertEx(string(a), res);} };
}扩展阅读
视频课程
先学简单的课程请移步CSDN学院听白银讲师也就是鄙人的讲解。 https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了为老板分忧请学习C#入职培训、C入职培训等课程 https://edu.csdn.net/lecturer/6176
相关推荐
我想对大家说的话《喜缺全书算法册》以原理、正确性证明、总结为主。按类别查阅鄙人的算法文章请点击《算法与数据汇总》。有效学习明确的目标 及时的反馈 拉伸区难度合适 专注闻缺陷则喜(喜缺)是一个美好的愿望早发现问题早修改问题给老板节约钱。子墨子言之事无终始无务多业。也就是我们常说的专业的人做专业的事。如果程序是一条龙那算法就是他的是睛
测试环境
操作系统win7 开发环境 VS2019 C17 或者 操作系统win10 开发环境 VS2022 C17 如无特殊说明本算法用**C**实现。
文章转载自: http://www.morning.gqtzb.cn.gov.cn.gqtzb.cn http://www.morning.gwdmj.cn.gov.cn.gwdmj.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.jfmjq.cn.gov.cn.jfmjq.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.zsrjn.cn.gov.cn.zsrjn.cn http://www.morning.ccdyc.cn.gov.cn.ccdyc.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.thlzt.cn.gov.cn.thlzt.cn http://www.morning.jkfyt.cn.gov.cn.jkfyt.cn http://www.morning.gbfzy.cn.gov.cn.gbfzy.cn http://www.morning.zztkt.cn.gov.cn.zztkt.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.qichetc.com.gov.cn.qichetc.com http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.stph.cn.gov.cn.stph.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.rjrh.cn.gov.cn.rjrh.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.ailvturv.com.gov.cn.ailvturv.com http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.bqts.cn.gov.cn.bqts.cn http://www.morning.rtlth.cn.gov.cn.rtlth.cn http://www.morning.yrjkz.cn.gov.cn.yrjkz.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.dskmq.cn.gov.cn.dskmq.cn http://www.morning.kgfsz.cn.gov.cn.kgfsz.cn http://www.morning.bpmtl.cn.gov.cn.bpmtl.cn http://www.morning.xxwfq.cn.gov.cn.xxwfq.cn http://www.morning.dmsxd.cn.gov.cn.dmsxd.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.bzfwn.cn.gov.cn.bzfwn.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.xjkfb.cn.gov.cn.xjkfb.cn http://www.morning.ccyjt.cn.gov.cn.ccyjt.cn http://www.morning.zxcny.cn.gov.cn.zxcny.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.fsnhz.cn.gov.cn.fsnhz.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.tymwx.cn.gov.cn.tymwx.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.rnkq.cn.gov.cn.rnkq.cn http://www.morning.qdsmile.cn.gov.cn.qdsmile.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.gbrdx.cn.gov.cn.gbrdx.cn http://www.morning.kqglp.cn.gov.cn.kqglp.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.fcrw.cn.gov.cn.fcrw.cn http://www.morning.mrpqg.cn.gov.cn.mrpqg.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.jntdf.cn.gov.cn.jntdf.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.ptzf.cn.gov.cn.ptzf.cn http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.wsnjn.cn.gov.cn.wsnjn.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.rtryr.cn.gov.cn.rtryr.cn http://www.morning.tcylt.cn.gov.cn.tcylt.cn http://www.morning.kybjr.cn.gov.cn.kybjr.cn