当前位置: 首页 > news >正文

通配*|滚动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;

}

};

http://www.cnnetsun.cn/news/35265.html

相关文章:

  • FBCTF平台管理终极指南:从零搭建到高效运营的完整攻略
  • 57、Python网络编程:客户端模块与URL访问
  • 61、Python CGI编程与替代方案全解析
  • Blender UI组件完整教程:从入门到精通打造专业3D界面
  • 3分钟快速安装Kali:虚拟机方案全解析
  • vue基于Spring Boot的旅游服务系统盘锦文旅系统设计与实现_6gvm8m81-java毕业设计
  • 5分钟解锁机械臂智能控制:从零到精通的AI实践指南
  • vue基于Spring Boot的自助点餐系统_z09ak8v7-java毕业设计
  • 5分钟掌握Papirus符号链接生成器:Linux图标管理终极指南
  • U-2-Net实战指南:打造智能图像分割利器
  • MySQL 知识点复习- 6. inner/right/left join
  • 对比传统方法:AI如何更高效解决wsappx资源问题
  • Netty入门指南:5分钟搭建你的第一个网络应用
  • 欧拉筛选法求质数的算法解析
  • 15、探索 Red Hat Linux 的实用功能与娱乐体验
  • 基于Simulink仿真的电动汽车模型构建与参数初始化研究
  • JavaScript数组push方法:小白也能懂的入门指南
  • IsaacLab机器人仿真系统实战配置指南:从零到专业部署
  • WeekToDo终极指南:如何快速搭建免费的周计划待办事项应用
  • 25、计算机硬件与Linux文件系统全解析
  • 28、Red Hat Linux 9:软件管理、系统配置与网络安全指南
  • AI如何帮你轻松实现Python包的本地开发模式
  • AI如何帮你快速掌握Netty框架的核心功能
  • Protobuf零基础入门:用快马平台10分钟完成第一个.proto文件
  • 基于SpringBoot的旧物回收商城系统的设计与实现计算机毕业设计项目源码文档
  • python测试1
  • Cloudpods多云管理平台:从零构建企业级混合云解决方案
  • OpenNMS快速入门指南:10分钟掌握开源网络管理核心技术
  • 比传统方法快10倍:并行化蚁群算法的性能突破
  • 游戏引擎里的世界管家