news 2026/6/23 0:13:23

通配*|滚动hash

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
通配*|滚动hash

lc2489

lc1983

差值前缀和+枚举右维护左

class Solution {
public:
int widestPairOfIndices(vector<int>& nums1, vector<int>& nums2)
{
int n=nums2.size();
vector<int> d(n+1);
unordered_map<int,int> hash;
hash[0]=0;//init
int ret=0;
for(int i=1;i<=n;i++)
{
d[i]=d[i-1]+nums1[i-1]-nums2[i-1];
if(hash.count(d[i]))
ret=max(ret,i-hash[d[i]]);
else
hash[d[i]]=i;
}
return ret;
}
};

lc3606

先按下标排序,在转换为字符串

class Solution {
public:
vector<string> validateCoupons(vector<string>& code, vector<string>& businessLine, vector<bool>& isActive)
{
int n=code.size();
vector<int> ret;
set<string> set={"electronics","grocery","pharmacy","restaurant"};
unordered_map<string,int> hash;
unordered_map<int,string> hash2;

for(int i=0;i<n;i++)
{
if(isActive[i]==true)
{
if(set.count(businessLine[i]))
{
bool check=true;
string tmp=code[i];
for(int j=0;j<code[i].size();j++)
{
if(isalnum(tmp[j]) || tmp[j]=='_')
check=true;
else
{
check=false;
break;
}
}
if(check && !tmp.empty())
{
ret.push_back(i);
}
}
}
}


sort(ret.begin(),ret.end(),[&](int& a,int& b)
{
string b1=businessLine[a],b2=businessLine[b];
if(b1!=b2)
{
return b1<b2;
}
return code[a]<code[b];
});
//先按下标排序,在转换为字符串
vector<string> ans;
for(auto& r:ret)
ans.push_back(code[r]);

return ans;

}
};

lc1554

滚动hash优化

unordered_map<int, vector<int>> hs;

hash给每个字符串“遮掉一位”做标记

遇到重复标记时,真实验证这俩串是不是只遮掉的那一位不同,是就返回true

class Solution {
public:
bool differByOne(vector<string>& dict) {
// self-defined hash, mod 5801, any big prime fine
// strict, check if there is hash clashing

int mod(5801), m(dict[0].length()), mod_pows[m];
mod_pows[0] = 1;
for (int i = 1; i < m; ++i)
mod_pows[i] = mod_pows[i - 1] * 27 % mod;

unordered_map<int, vector<int>> hs; // we can also use deque<int> here

for (int k = 0; k < dict.size(); ++k) {
int h = 0;
for (char& c: dict[k])
h = (27 * h + c - 'a' + 1) % mod;
for (int i = 0; i < m; ++i) {
int t = (h - mod_pows[m - i - 1] * (dict[k][i] - 'a' + 1) % mod + mod) % mod;
if (hs.count(t)) {
for (const int& x: hs[t]) {
int kk(x / m), ii(x % m);
if (ii == i) {
bool checked = true;
for (int p = 0; p < m; ++p) {
if (p == i) continue;
if (dict[k][p] != dict[kk][p]) {
checked = false;
break;
}
}
if (checked) return true;
}
}
}
hs[t].push_back(m * k + i);
}
}

return false;
}
};

mle 通配* hash

class Solution {

public:

bool differByOne(vector<string>& dict) {

if (dict.empty()) return false;

int len = dict[0].size();

unordered_set<string> seen;

for (auto& s : dict) {

// 通配* hash

for (int i = 0; i < len; ++i) {

string key = s;

key[i] = '*'; // 把第i位替换为通配符,代表忽略该位的差异

if (seen.count(key)) {

return true;

}

seen.insert(key);

}

}

return false;

}

};

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/20 20:07:01

FBCTF平台管理终极指南:从零搭建到高效运营的完整攻略

在当今网络安全竞赛蓬勃发展的时代&#xff0c;如何高效管理一个CTF平台成为众多技术管理员面临的挑战。本指南将带你深入了解FBCTF这一专业级CTF竞赛平台的管理技巧&#xff0c;助你轻松应对从平台部署到竞赛运营的全流程工作。 【免费下载链接】fbctf Platform to host Captu…

作者头像 李华
网站建设 2026/6/22 14:29:09

57、Python网络编程:客户端模块与URL访问

Python网络编程:客户端模块与URL访问 在网络编程中,程序可以作为客户端(访问资源的程序)或服务器(提供服务的程序)运行。客户端和服务器程序都需要处理协议问题(如如何访问和传输数据)以及数据格式问题。Python 库通过多个模块来处理这些问题,本文将重点介绍支持客户…

作者头像 李华
网站建设 2026/6/21 22:10:09

61、Python CGI编程与替代方案全解析

Python CGI编程与替代方案全解析 1. Python中的CGI编程基础 CGI(Common Gateway Interface)标准允许使用任何语言编写CGI脚本,而Python作为一种高级、高生产力的语言,非常适合用于CGI编程。Python标准库提供了处理典型CGI相关任务的模块。 2. 表单提交方法 CGI脚本常处…

作者头像 李华
网站建设 2026/6/23 17:34:44

Blender UI组件完整教程:从入门到精通打造专业3D界面

Blender UI组件完整教程&#xff1a;从入门到精通打造专业3D界面 【免费下载链接】awesome-blender &#x1fa90; A curated list of awesome Blender addons, tools, tutorials; and 3D resources for everyone. 项目地址: https://gitcode.com/GitHub_Trending/aw/awesome…

作者头像 李华
网站建设 2026/6/22 2:52:26

3分钟快速安装Kali:虚拟机方案全解析

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发虚拟机快速部署工具&#xff0c;功能&#xff1a;1.一键导入预配置的Kali虚拟机模板 2.自动分配优化资源&#xff08;CPU/内存/磁盘&#xff09; 3.内置常用工具包 4.支持快照管…

作者头像 李华