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 ...
随机推荐
- Linux下多路径multipath配置【转】
一.multipath在redhat 6.2中的基本配置: 1. 通过命令:lsmod |grep dm_multipath 检查是否正常安装成功.如果没有输出说明没有安装那么通过yum功能安装一下 ...
- Machine Learning系列--CRF条件随机场总结
根据<统计学习方法>一书中的描述,条件随机场(conditional random field, CRF)是给定一组输入随机变量条件下另一组输出随机变量的条件概率分布模型,其特点是假设输出 ...
- 多播知识by 陈胜君
简单的讲一下多拨的说明:一.多拨分物理多拨和虚拟多拨. 物理多拨是电信老套餐,就是一个宽带支持四个内网设备同时拨号上网,即2004年以前,允许家里四台电脑直接连LAN网口启动拨号,同时允许四拨在线.现 ...
- RestTemplate OR Spring Cloud Feign 上传文件
SpringBoot,通过RestTemplate 或者 Spring Cloud Feign,上传文件(支持多文件上传),服务端接口是MultipartFile接收. 将文件的字节流,放入ByteA ...
- idea关于断点的补充
黑背景版: 先编译好要调试的程序.1.设置断点
- 轻量级运维工具-pssh,pscp,prsync,pslurp,pnuke
批量执行,并行传输,远程killall #yum安装 yum install pssh -y #yun安装后pscp改名为pscp.pssh #源码编译 wget https://pypi.pytho ...
- php直接输出json格式
php直接输出json格式,很多新手有一个误区,以为用echo json_encode($data);这样就是输出json数据了,没错这样输出文本是json格式文本而不是json数据,正确的写法是应该 ...
- centos6.5 安装、启动vnc
一.安装vnc 1.确保当前账号是root2.查看本机是否已经安装vncserver rpm -qa|grep tigervnc 3.安装vncserver yum -y install tigerv ...
- 读书笔记|Windows 调试原理学习|持续更新
关于调试方面的学习笔记,主要来源于<软件调试>的读书笔记和梦织未来论坛的视频教程 1.调试器使用一个死循环监听调试信息. DebugActiveProcess(PID);while(TRU ...
- 号外,号外 -几乎所有的binary search和mergesort都有错
号外,号外 -几乎所有的binary search和mergesort都有错 这是Joshua Bloch(Effective Java的作者)在google blog上发的帖子.在说这个帖子之前,不 ...