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 ...
随机推荐
- WordPress友情链接插件的安装
插件:link manager 这样就安装成功! 在外观小工具里 把右边即可 这样在前台页面上就可看见添加的友情链接了!!!
- 设计模式之Composite
设计模式总共有23种模式这仅仅是为了一个目的:解耦+解耦+解耦...(高内聚低耦合满足开闭原则) Composite定义? 将对象以树形结构组织起来,以达成“部分-整体” 的层次结构. 想到Compo ...
- Java爬虫(二)
上一篇简单的实现了获取url返回的内容,在这一篇就要第返回的内容进行提取,并将结果保存到html中.而且这个爬虫是基于python爬虫的java语言实现,其逻辑大致相同. 一 . 需求: 抓取主页面: ...
- ELK&ElasticSearch5.1基础概念及配置文件详解【转】
1. 配置文件 elasticsearch/elasticsearch.yml 主配置文件 elasticsearch/jvm.options jvm参数配置文件 elasticsearch/log4 ...
- Linux轻量级自动运维工具-Ansible浅析【转】
转自 Linux轻量级自动运维工具-Ansible浅析 - ~微风~ - 51CTO技术博客http://weiweidefeng.blog.51cto.com/1957995/1895261 Ans ...
- 关于DataTable.Select不到数据的一种解决方案
网上有很多说的,试过,都没用.自己研究了一下,解决方案如下: 建立dataview,用dv.rowfilter,就可以取到了,然后TOTABLE即可. 代码如下:(只看中间那几句即可) private ...
- UVA题解一
UVA 100 题目描述:经典3n+1问题在\(n \leq 10^6\)已经证明是可行的,现在记\(f[n]\)为从\(n\)开始需要多少步才能到\(1\),给出\(L, R\),问\(f[L], ...
- C/C++——[05] 函数
函数是 C/C++语言中的一种程序组件单位.一个函数通常代表了一种数据处理的功能,由函数体和函数原型两部分组成.函数原型为这个数据处理功能指定一个标识符号(函数的名称).说明被处理数据的组成及其类型. ...
- apache kafka系列之jmx监控指标参数
https://blog.csdn.net/lizhitao/article/details/35986849
- 全连通图求最小生成树边权之积(邻接矩阵/prim/kruskal)
Description 大家都知道最小生成树一般求的是构成最小生成树的边的权值之和. 现在请求构成最小生成树的边的权值之积 S,最终结果请输出 (S % 100003). P.S. 点之间的边为无向边 ...