HDU 4618 Palindrome Sub-Array 暴力
Palindrome Sub-Array
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=4618
Description
A palindrome sequence is a sequence which is as same as its reversed order. For example, 1 2 3 2 1 is a palindrome sequence, but 1 2 3 2 2 is not. Given a 2-D array of N rows and M columns, your task is to find a maximum sub-array of P rows and P columns, of which each row and each column is a palindrome sequence.
Input
The first line of input contains only one integer, T, the number of test cases. Following T blocks, each block describe one test case.
There is two integers N, M (1<=N, M<=300) separated by one white space in the first line of each block, representing the size of the 2-D array.
Then N lines follow, each line contains M integers separated by white spaces, representing the elements of the 2-D array. All the elements in the 2-D array will be larger than 0 and no more than 31415926.
Output
For each test case, output P only, the size of the maximum sub-array that you need to find.
Sample Input
1
5 10
1 2 3 3 2 4 5 6 7 8
1 2 3 3 2 4 5 6 7 8
1 2 3 3 2 4 5 6 7 8
1 2 3 3 2 4 5 6 7 8
1 2 3 9 10 4 5 6 7 8
Sample Output
4
Hint
题意
给你一个n,m的矩形,你需要找到一个最大正方形,使得每一行每一列都是回文的
输出正方形的边长
题解:
首先把奇数位置都填上-1,然后这样所有回文的东西都是奇数长度了
暴力预处理出以每一个数为中心,能够向上,向左边扩展多少
然后我们在n^2暴力枚举回文正方形的中心点,再O(N)的去check最大能扩展多少就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 701;
int M[maxn][maxn];
int M2[maxn][maxn];
int Len1[maxn][maxn],Len2[maxn][maxn];
int n,m;
void init()
{
memset(M,0,sizeof(M));
memset(M2,0,sizeof(M2));
memset(Len1,0,sizeof(Len1));
memset(Len2,0,sizeof(Len2));
}
void solve()
{
init();
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&M2[i][j]);
for(int i=1;i<=2*n+1;i++)
{
for(int j=1;j<=2*m+1;j++)
{
if(i%2==1||j%2==1)M[i][j]=-1;
else M[i][j]=M2[i/2][j/2];
}
}
n=n*2+1;
m=m*2+1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int l = 0;
for(int k=0;j-k>=1&&j+k<=m;k++)
{
if(M[i][j-k]==M[i][j+k])
l=k;
else
break;
}
Len1[i][j]=l;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int l = 0;
for(int k=0;i+k<=n&&i-k>=1;k++)
{
if(M[i+k][j]==M[i-k][j])
l=k;
else
break;
}
Len2[i][j]=l;
}
}
int ans = 0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int Min1 = 1<<30;
int Min2 = 1<<30;
for(int l=0;i-l>=1&&i+l<=n&&j+l<=m&&j-l>=1;l++)
{
Min1=min(Min1,Len2[i][j-l]),Min1=min(Min1,Len2[i][j+l]);
Min2=min(Min2,Len1[i+l][j]),Min2=min(Min2,Len1[i-l][j]);
if(Min1<l||Min2<l)break;
if(M[i][j]==-1)ans = max(ans,l);
else ans = max(ans,l);
}
}
}
cout<<ans<<endl;
return;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}
HDU 4618 Palindrome Sub-Array 暴力的更多相关文章
- HDU 4618 Palindrome Sub-Array (2013多校2 1008 暴力)
Palindrome Sub-Array Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
- hdu 4618 Palindrome Sub-Array
http://acm.hdu.edu.cn/showproblem.php?pid=4618 直接DP+记忆化 虽然时间复杂度看起来是300^4 但实际执行起来要远远小于这个值 所有可以水过 代码: ...
- HDU 4618 Palindrome Sub-Array(DP)
题目链接 我还是图样啊....比赛的时候没敢暴力去搜... #include <cstdio> #include <cstdlib> #include <cstring& ...
- HDU 5280 Senior's Array (暴力,水)
题意:给一个数列,再给一个数字p,要求p一定要替换掉数列中的一个元素,然后求最大连续子序列之和. 思路:1000*1000的复杂度,O(n*n) .就是每个都试,然后求和. #include < ...
- HDU 4618 - Palindrome Sub-Array(2013MUTC2-1008)(DP)
d(i,j,k)表示左上角坐标为(i,j),k为正方形边长 d(i,j,k)=1,如果d(i+1,j+1,k-2)=0,且上下两个外围的相等且回文,左右两个外围的相等且回文:否则d(i,j,k)=0 ...
- hdu 1159 Palindrome(回文串) 动态规划
题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...
- HDU 2920 分块底数优化 暴力
其实和昨天写的那道水题是一样的,注意爆LL $1<=n,k<=1e9$,$\sum\limits_{i=1}^{n}(k \mod i) = nk - \sum\limits_{i=1}^ ...
- HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛
普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...
- HDU 4749 Parade Show(暴力水果)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 Problem Description 2013 is the 60 anniversary ...
随机推荐
- sqlalchemy更新和插入操作
def save_app_info(self): try: # update app_info print(self.dicts) data = db_session.query(App_Info). ...
- 从输入URL到显示页面的过程分析
作为一个软件开发者,你一定会对网络应用如何工作有一个完整的层次化的认知,同样这里也包括这些应用所用到的技术:像浏览器,HTTP,HTML,网络服务器,需求处理等等. 本文将更深入的研究当你输入一个网址 ...
- 关闭windows 7的自动休眠功能
powercfg -h off powercfg -h on https://www.tulaoshi.com/n/20160401/2075397.html powercfg -h on 该文章&l ...
- Linux平台上SQLite数据库教程(一)——终端使用篇
https://blog.csdn.net/u011192270/article/details/48031763 https://blog.csdn.net/fml1997/article/deta ...
- 两天撸一个天气应用微信小程序
更新说明: I.气象数据由百度地图开放平台修改为了和风天气,需要注册账号获取 key: II.d0e51c8 版本之后为小程序云开发版本,若未开通云开发功能,为不影响小程序正常运行,可以将版本号回退到 ...
- JavaScript中的数据类型总结
Javascript是一种弱类型语言,没有明确的类型分类:网上分类的方式比较多,个人感觉不比去特别的追究细分是什么什么类型,若是能够明确的分出类型的话,javascript就不是弱类型语言,又由于大家 ...
- linux java环境变量设置
下载JRE或者JDK后解压,设置以下环境变量 JAVA_HOME=/home/zm/jdk1.8.0_181JRE_HOME=/home/zm/jdk1.8.0_181/jreCLASSPATH=.: ...
- SQLyog-12.0.9-0.x64
SQLyog-12.0.9-0.x64 下载地址 链接:https://pan.baidu.com/s/1AjC2rdx8Bgtns1sYmN5NEw 提取码:icko 注册码: 姓名(Name):c ...
- thinkphp5.0URL访问
URL设计 ThinkPHP5.0在没有启用路由的情况下典型的URL访问规则是: http://serverName/index.php(或者其它应用入口文件)/模块/控制器/操作/[参数名/参数值. ...
- 微信用户授权,获取code
1.进入微信公众平台 2.进入到 开发->接口授权,点击 网页服务->网页授权->网页授权获取用户基本信息 后面的“修改“. 3.点击网页授权域名的设置 4.设置授权回调域名 ...