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 暴力的更多相关文章

  1. 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 ...

  2. hdu 4618 Palindrome Sub-Array

    http://acm.hdu.edu.cn/showproblem.php?pid=4618 直接DP+记忆化 虽然时间复杂度看起来是300^4 但实际执行起来要远远小于这个值 所有可以水过 代码: ...

  3. HDU 4618 Palindrome Sub-Array(DP)

    题目链接 我还是图样啊....比赛的时候没敢暴力去搜... #include <cstdio> #include <cstdlib> #include <cstring& ...

  4. HDU 5280 Senior's Array (暴力,水)

    题意:给一个数列,再给一个数字p,要求p一定要替换掉数列中的一个元素,然后求最大连续子序列之和. 思路:1000*1000的复杂度,O(n*n) .就是每个都试,然后求和. #include < ...

  5. 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 ...

  6. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...

  7. HDU 2920 分块底数优化 暴力

    其实和昨天写的那道水题是一样的,注意爆LL $1<=n,k<=1e9$,$\sum\limits_{i=1}^{n}(k \mod i) = nk - \sum\limits_{i=1}^ ...

  8. HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...

  9. HDU 4749 Parade Show(暴力水果)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 Problem Description   2013 is the 60 anniversary ...

随机推荐

  1. bootstrap带图标的按钮与图标做连接

    bootstrap通过引入bootstrap的JS与css文件,给元素添加class属性即可. 使用图标只需要加入一个span,class属性设置为对应的图标属性即可.图标对应的class属性可以参考 ...

  2. centos7安装完成后的一些配置

    1.打开终端 输入 sudo yum -y update 先更新软件包 2.这是输入语言 应用程序->系统工具->设置->区域和语言->+   ->汉语(中国)-> ...

  3. java.lang.IllegalArgumentException: Page directive: invalid value for import

    我的项目原来用的tomcat版本是apache-tomcat-7.0.53,后来为了安全原因将版本升至\apache-tomcat-7.0.57,发现有的jsp页面出现下面的异常: java.lang ...

  4. Jmeter------将JDBC Request的查询结果作为另一个接口的请求参数

    一.前言 jmeter已配置连接成功数据库,不会的可查看:https://www.cnblogs.com/syw20170419/p/9832402.html 二.需求 将JDBC Request的r ...

  5. WMI技术介绍和应用——WMI概述

    https://blog.csdn.net/breaksoftware/article/details/8424317

  6. linux shell awk实现实时监控网卡流量脚本

    goodtools! 原文 awk 'BEGIN{ OFMT="%.3f"; devf="/proc/net/dev"; while(("cat &q ...

  7. 【C#日期系列(二)】--C#获取一段时间有多少个星期几

    #region 统计一段时间内有多少个星期几 ///<summary> ///统计一段时间内有多少个星期几 ///</summary> ///<param name=&q ...

  8. 【LOJ】 #2132. 「NOI2015」荷马史诗

    题解 k叉哈夫曼树,但是没有了二叉那样的最后一定能合并成一个树根的优秀性质,我们就不断模拟操作看看到了哪一步能用的节点数< k,然后先拿这些节点数合并起来 然后就可以k个k个合并了,大小一样先拿 ...

  9. U3D 基础

    千里之行,始于足下! 最先执行的方法是:1.(激活时的初始代码)Awake2.Start3.Update(FixUpdate,LateUpdate)4.渲染模块(OnGUI)5.再向后,就是卸载模块( ...

  10. 《java虚拟机》----java内存区域与内存溢出异常

    No1: java虚拟机所管理的内存将会包括以下几个运行时数据区域 1.方法区 2.虚拟机栈 3.本地方法栈 4.堆 5.程序计数器 No2: 程序计数器: 程序计数器(Program Counter ...