牛客网训练1--------矩阵 (二份+二维矩阵hash)
不懂hash的话:https://www.cnblogs.com/ALINGMAOMAO/p/10345850.html
思路:对于一个大矩阵的每一个子矩阵都对应着一个hash值k, 当k出现2次以上时就满足要求
只是对长度进行二分就可以了。
收获:学会了hash算法
#include<iostream> #include<map> using namespace std; #define ll long long #define ull unsigned long long const int maxn = 510; const ull base1 = 131; const ull base2 = 233; int n, m; char mp[maxn][maxn]; ull has[maxn][maxn]; ull p1[maxn], p2[maxn]; map<ull, int>mmp; void init(){ p1[0] = p2[0] = 1; for (int i = 1; i <= 505; ++i){ p1[i] = p1[i - 1] * base1; p2[i] = p2[i - 1] * base2; } } void Hash(){ has[0][0] = 0; has[0][1] = 0; has[1][0] = 0; for (int i = 1; i <= n;++i) for (int j = 1; j <= m; ++j){ has[i][j] = has[i][j-1] * base1 + mp[i][j]-'a'; } for (int i = 1; i <= n;++i) for (int j = 1; j <= m; ++j){ has[i][j] = has[i - 1][j] * base2 + has[i][j]; } } bool check(int x){ mmp.clear(); for (int i = x; i <= n; ++i) for (int j = x; j <= m; ++j){ ull k = has[i][j] - has[i - x][j] * p2[x] - has[i][j - x] * p1[x] + has[i - x][j-x] * p1[x]*p2[x]; mmp[k]++; if (mmp[k] >= 2)return true; } return false; } int main() { init(); while (cin >> n >> m){ for (int i = 1; i <= n; ++i) cin >> (mp[i] + 1); Hash(); int l = 0, r = 600, ans = 0; while (l <= r){ int mid = (l + r) / 2; if (check(mid)){ l = mid + 1; ans = mid; } else{ r = mid - 1; } } cout << ans << endl; } }
作者:ALINGMAOMAO
出处:https://www.cnblogs.com/ALINGMAOMAO/p/10345873.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探