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. MongoDB之安装和基本使用(一)

    环境 ubuntu16.04 mongodb基本特点 MongoDB 是一个基于分布式 文件存储的NoSQL数据库;可以把MongoDB想象成一个大py字典. 模式自由 :可以把不同结构的文档存储在同 ...

  2. Android的休眠与唤醒

    Android 休眠(suspend),在一个打过android补丁的内核中,state_store()函数会走另外一条路,会进入到request_suspend_state()中,这个文件在earl ...

  3. Python基础之杂货铺

    字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...

  4. HDU 6197 array array array 2017沈阳网络赛 LIS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6197 题意:给你n个数,问让你从中删掉k个数后(k<=n),是否能使剩下的序列为非递减或者非递增 ...

  5. Oracle安装出现报错

    报错信息如下: >>> Couldnot execute auto check for display colors using command /usr/bin/xdpyinfo. ...

  6. TypeError: not all arguments converted during string formatting

    print ("So, you're 5r old, %r tall and %r heavy." % (age, height, weight)) print ("So ...

  7. 使用ExtJS做一个用户的增删改查

    extjs版本为4.2,用户数据放在静态list中存储 User.java package com.ext.demo.dao; public class User { private int id; ...

  8. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...

  9. js写一个插件

    //;分号开头,用于防止代码压缩合并时与其它代码混在一起造成语法错误 //而事实证明,uglify压缩工具会将无意义的前置分号去掉,我只是习惯了这么写 //(function(){})();立即执行函 ...

  10. 洛谷 P1957 口算练习题 题解

    题目传送门 这道题是考字符串处理,另外输入要使用c++的cin的神奇功能. #include<bits/stdc++.h> using namespace std; int n;char ...