【牛客 -2A】矩阵(二分,字符串哈希)

题干:

给出一个n * m的矩阵。让你从中发现一个最大的正方形。使得这样子的正方形在矩阵中出现了至少两次。输出最大正方形的边长。

输入描述:

第一行两个整数n, m代表矩阵的长和宽;
接下来n行,每行m个字符(小写字母),表示矩阵;

输出描述:

输出一个整数表示满足条件的最大正方形的边长。

 

示例1

输入

复制

5 10
ljkfghdfas
isdfjksiye
pgljkijlgp
eyisdafdsi
lnpglkfkjl

输出

复制

3

备注:

对于30%的数据,n,m≤100;
对于100%的数据,n,m≤500;

解题报告:

   二分一下矩阵的边长,然后用字符串哈希判断两个矩阵是否相同就可以,,网络上找到一个map的解法,,但是感觉时间复杂度太不稳定,好的时候700ms,差的时候超时,,于是还是以后用多项式哈希吧。。这两个刚学哈希,,代码还是不特别美观、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 505;
using namespace std;
const ll D1=31,D2=131;
int n,m;
char a[MAX][MAX];
ull pow1[MAX],pow2[MAX],h[MAX][MAX],tmp,tmp2,Hash[MAX*MAX];
bool ok(int x) {memset(h,0,sizeof h);int tot=0;for(int i = 1; i<=n; i++) {ull tmp = 0;for(int j = 1; j<x; j++) h[i][j] = h[i][j-1] * D1 + a[i][j];for(int j = x; j<=m; j++) h[i][j] = h[i][j-1] * D1 + a[i][j] - pow1[x] * a[i][j-x];
//		for(int j = 1; j<x; j++) tmp = tmp * D1 + a[i][j];
//		for(int j = x; j<=m; j++) h[i][j] = tmp * D1 + a[i][j] - pow1[x] * a[i][j-x] , tmp = h[i][j];}for(int j = x; j<=m; j++) {ull tmp = 0;for(int i = 1; i<x; i++) tmp = tmp*D2 + h[i][j];for(int i = x; i<=n; i++) Hash[++tot] = tmp*D2 + h[i][j] - pow2[x] * h[i-x][j] , tmp = Hash[tot];}sort(Hash+1,Hash+tot+1);for(int i = 1; i<tot; i++) {if(Hash[i] == Hash[i+1]) return 1;}return 0 ;
}
int main() 
{
//	ull qq=1,ww=2;
//	cout <<qq-ww;scanf("%d%d",&n,&m);for(int i=1; i<=n; i++) {scanf("%s",a[i]+1);for(int j=1; j<=m; j++) {a[i][j]-=('a'-1);}}int l=1,r=min(n,m);int mid = (l+r)>>1;int ans;pow1[0]=pow2[0]=1;for(int i=1; i<=n; i++) pow1[i]=pow1[i-1]*D1;for(int i=1; i<=m; i++) pow2[i]=pow2[i-1]*D2;while(l<=r) {mid=(l+r)>>1;if(ok(mid)) l=mid+1,ans=mid;else r=mid-1;	}printf("%d",ans);return 0;
}

AC代码2:(map版)(这个时不时会跑个超时、。。)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int>P;
#define maxn 505
int n,m;
char s[maxn][maxn];
ull h[maxn][maxn],b[maxn*maxn],base=10007,hhh[maxn][maxn];
inline int ID(int i,int j) {return (i-1)*m+j;
}
inline bool check(int mid) {map<ull,int>M;for(int x=1; x<=n-mid+1; x++)for(int y=1; y<=m-mid+1; y++) {int xx=x+mid-1,yy=y+mid-1;ull val=(h[xx][yy]-h[xx][y-1]-h[x-1][yy]+h[x-1][y-1])*b[(n-x)*m+m-y];if(M.find(val)!=M.end())return 1;M[val]=1;}return 0;
}
int main() {scanf("%d%d",&n,&m);for(int i=1; i<=n; i++)scanf("%s",s[i]+1);b[0]=1;for(int i=1; i<=n; i++)for(int j=1; j<=m; j++) {h[i][j]=h[i][j-1]+b[ID(i,j-1)]*(s[i][j]-'a');b[ID(i,j)]=b[ID(i,j-1)]*base;}
//	for(int j=1; j<=m; j++)
//		for(int i=1; i<=n; i++)
//			h[i][j]+=h[i-1][j];int l=0,r=min(n,m),mid,ans=0;while(l<=r) {mid=(l+r)/2;if(check(mid))ans=mid,l=mid+1;else r=mid-1;}printf("%d\n",ans);return 0;
}

总结:据syt表示,哈希题目多半带个log,,这次做题果然是,需要比较,所以需要先排序这样。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441119.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java生产者消费者代码_Java实现Kafka生产者消费者代码实例

Kafka的结构与RabbitMQ类似&#xff0c;消息生产者向Kafka服务器发送消息&#xff0c;Kafka接收消息后&#xff0c;再投递给消费者。生产者的消费会被发送到Topic中&#xff0c;Topic中保存着各类数据&#xff0c;每一条数据都使用键、值进行保存。每一个Topic中都包含一个或多…

java dom创建xml文件_Java 如何使用dom方式读取和创建xml文件

Java 如何使用dom方式读取和创建xml文件发布时间&#xff1a;2020-11-11 17:08:31来源&#xff1a;亿速云阅读&#xff1a;101作者&#xff1a;Leah本篇文章给大家分享的是有关Java 如何使用dom方式读取和创建xml文件&#xff0c;小编觉得挺实用的&#xff0c;因此分享给大家学…

【CodeForces - 304B】Calendar (前缀和,水题)

题干&#xff1a; Calendars in widespread use today include the Gregorian calendar, which is the de facto international standard, and is used almost everywhere in the world for civil purposes. The Gregorian reform modified the Julian calendars scheme of le…

java 刷新jtextarea_Java JTextArea不能实时刷新的问题

相信JTextArea用法都了解吧&#xff0c;JTextArea textArea new JTextArea();生成一块空白的区域&#xff0c; 我的需求就是点击发送邮件按钮后&#xff0c;后台的执行日志能输出到textArea中。但是我点击发送按钮的时候&#xff0c;由于邮件的附件要好久&#xff0c;界面一直…

【CodeForces - 312C】The Closest Pair (思维)

题干&#xff1a; Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Excee…

java request 封装对象_java通过request自动封装复杂对象

参考&#xff1a;Jfinal源码&#xff0c;在上面基础上改的&#xff0c;然后分享出来适用JAVAEE平台[Java]代码/*** 实现深层封装对象的实例 从request封装对象* 举例&#xff1a;* House.class 属性有三个 ID:id 名称&#xff1a;name 门类&#xff1a;Door doorDoor类: id nam…

【UVA - 10020 】Minimal coverage (贪心,区间覆盖问题)

题干&#xff1a;&#xff08;Uva题不给题干了&#xff09; t组样例&#xff0c;每组首先给出一个M&#xff0c;然后给出一些线段&#xff08;0 0结束&#xff09;&#xff0c;然后问怎么取能使得最少的线段覆盖区间[0, M]。 Sample Input 2 1 -1 0 -5 -3 2 5 0 0 1 -1 0 0 1 …

java邮箱地址正则表达式_Java 中用正则表达式修改 Email 地址

需求系统中有一列会用来存储 email 地址&#xff0c;现在需要对输入的字符串进行过滤&#xff0c;要求是&#xff0c;把无效的地址过滤掉。有一些需要说明的是这些地址是通过图像识别得到的&#xff0c;有些是用户自己输入的已有历史记录已经存在了脏数据&#xff0c;需要替换这…

【CodeForces - 305C】Ivan and Powers of Two(思维,STL,set,优先队列)

题干&#xff1a; Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number …

【CodeForces - 1082B】Vova and Trophies (贪心模拟,暴力)

题干&#xff1a; Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row. The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wan…

java mac 转换 整形_JAVA的整型与字符串相互转换

1如何将字串 String 转换成整数 int?A. 有两个方法:1). int i Integer.parseInt([String]); 或i Integer.parseInt([String],[int radix]);2). int i Integer.valueOf(my_str).intValue();注: 字串转成 Double, Float, Long 的方法大同小异.2 如何将整数 int 转换成字串 St…

【CodeForces - 798A】Mike and palindrome (回文串,水题,字符串问题)

题干&#xff1a; Mike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome. A palindrome is a string that reads the same backward as forward, for e…

java迷宫生成算法_DFS算法迷宫生成器

我正在尝试使用DFS算法在ASCII中创建迷宫(&#xff03;表示墙和自由空间)&#xff0c;其左上角开始&#xff0c;右下角出口 . 问题是迷宫开始创建&#xff0c;然后它被阻止&#xff0c;因为它的所有邻居都已被访问过 .我从左上角开始&#xff0c;将单元格标记为已访问并放置一个…

【牛客 - 272A】Phrase String(构造,水题)

题干&#xff1a; 给出v, k&#xff0c;请你找到最小的正整数n&#xff0c;满足&#xff1a; n的二进制表示下存在一个长度为v的回文串&#xff0c;该回文串首尾都是1且n的二进制表示中至少有k个1。保证v,k均为偶数&#xff01; 由于n可能很大&#xff0c;你只需要输出对取模的…

eclipse 跑maven命令_maven编写命令行执行mvn package没问题,eclipse执行报错

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼报错是这样的“[ERROR] Unknown lifecycle phase "Systemout3". You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sou…

【CodeForces - 798D】Mike and distribution (思维构造,贪心,黑科技)

题干&#xff1a; Mike has always been thinking about the harshness of social inequality. Hes so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A  [a1, a2, ..., an…

erlang odbc mysql参数_Erlang在Redhat 5.3下使用unixODBC连接Oracle数据库的配置

个人在安装配置时遇到一些麻烦&#xff0c;特此记录如下&#xff1a; 环境 数据库服务器操作系统&#xff1a;Windows 2003 数据库&#xff1a;Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 erlang运行的服务器操作系统&#xff1a;Redhat 5.3 erlang:Erlang R1…

【POJ - 2533】Longest Ordered Subsequence(四种方法解决最长上升子序列 含二分优化版本)

题干&#xff1a; Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 41944 Accepted: 18453 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any se…

halcon图片上传到mysql_C# 10个线程并发执行Halcon图像算法 报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”...

如题&#xff0c;这个问题本人已经纠结了快三个工作日了。本人不同WinFrom程序一起动就会开启10个线程&#xff0c;并发对10张图片进行算法处理&#xff0c;问题是只要程序一起动就会报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”异常。本人试过将8个线程停掉…

【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)

题干&#xff1a; Constructing Roads In JGShinings Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29933 Accepted Submission(s): 8496 Problem Description JGShinings kingdom consists of 2n…