牛客网训练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 = ;
const ull base1 = ;
const ull base2 = ;
int n, m;
char mp[maxn][maxn];
ull has[maxn][maxn];
ull p1[maxn], p2[maxn];
map<ull, int>mmp; void init(){
p1[] = p2[] = ;
for (int i = ; i <= ; ++i){
p1[i] = p1[i - ] * base1;
p2[i] = p2[i - ] * base2;
}
}
void Hash(){
has[][] = ;
has[][] = ;
has[][] = ;
for (int i = ; i <= n;++i)
for (int j = ; j <= m; ++j){
has[i][j] = has[i][j-] * base1 + mp[i][j]-'a';
}
for (int i = ; i <= n;++i)
for (int j = ; j <= m; ++j){
has[i][j] = has[i - ][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] >= )return true;
}
return false;
} int main()
{
init();
while (cin >> n >> m){
for (int i = ; i <= n; ++i)
cin >> (mp[i] + );
Hash();
int l = , r = , ans = ;
while (l <= r){
int mid = (l + r) / ;
if (check(mid)){
l = mid + ;
ans = mid;
}
else{
r = mid - ;
}
}
cout << ans << endl;
}
}
牛客网训练1--------矩阵 (二份+二维矩阵hash)的更多相关文章
- 牛客网第二场Jfarm(随机化+二维前缀和)
链接:https://www.nowcoder.com/acm/contest/140/J 来源:牛客网 White Rabbit has a rectangular farmland of n*m. ...
- 牛客网训练赛26D(xor)
题目链接:https://www.nowcoder.com/acm/contest/180/D 线性基的学习:https://www.cnblogs.com/vb4896/p/6149022.html ...
- 牛客网-《剑指offer》-二进制中1的个数
题目:http://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8 C++ 负数需要特殊处理,因为负数右移会补1(符号位) cla ...
- 牛客网-《剑指offer》-二维数组中的查找
题目:http://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e C++ class Solution { public: bo ...
- 顺时针打印矩阵 牛客网 剑指Offer
顺时针打印矩阵 牛客网 剑指Offer 题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 ...
- 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)
题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 C.二元-K个二元组最小值和最大-优先队列+贪心(思维)
链接:https://ac.nowcoder.com/acm/contest/558/C来源:牛客网 小猫在研究二元组. 小猫在研究最大值. 给定N个二元组(a1,b1),(a2,b2),…,(aN, ...
- 算法题 19 二叉平衡树检查 牛客网 CC150
算法题 19 二叉平衡树检查 牛客网 CC150 实现一个函数,检查二叉树是否平衡,平衡的定义如下,对于树中的任意一个结点,其两颗子树的高度差不超过1. 给定指向树根结点的指针TreeNode* ro ...
- 牛客网多校训练第二场D Kth Minimum Clique
链接:https://ac.nowcoder.com/acm/contest/882/D来源:牛客网 Given a vertex-weighted graph with N vertices, fi ...
随机推荐
- overall error
Overall error is same with total error in math.
- Request method 'POST' not supported错误和解决方法
在使用SpringBoot的时候,在html页面用form表单post提交数据的时候报错: Request method 'POST' not supported 错误解析: 我是用的前端页面是HTM ...
- K8S dashboard
kubernetes-dashboard有两种认证方式,一个token认证,另一个是Kubeconfig文件的认证.这个时候的认证不是UserAccount而是获取kubernetes集群资源信息的s ...
- js 人民币小写金额转换为大写
function smalltoBIG(n) { var fraction = ['角', '分']; var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', ...
- vuejs2.0运用原生js实现简单的拖拽元素功能
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...
- centos 安装mysql Package: akonadi-mysql-1.9.2-4.el7.x86_64 (@anaconda)
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-release ...
- IBM沃森会成为第一个被抛弃的AI技术吗?
作者|William Vorhies 译者|姚佳灵 编辑|Debra 导读:IBM 的沃森问答机(Question Answering Machine,简称 QAM),因 2011 年参加综艺节目&l ...
- Java synchronized解析
多线程三大特性: 可见性.原子性.有序性 synchronize的特性: 1.同一时刻只有一个线程访问临界资源 2.其它未获取到锁执行权的线程必须排队等待 3.保证共享资源的原子性.可见性和有序性 4 ...
- QTP入门——玩玩小飞机
1.什么是QTP? 百度百科中对QTP是这么介绍的: ——”QTP是QuickTest Professional的简称,是一种自动化测试工具.使用QTP的目的是想用它来执行重复的自动化测试,主要是用于 ...
- (网页)JavaScript周末总结(一)
本周学习的内容总结: 1. 2章在html中使用javascript. 2. 3章Javascript的基本概念. 3. 4章变量,作用域,内存问题. 1-1:2章以下内容: 1.包含javascri ...