hdu4618(KMP+动态规划)

Palindrome Sub-Array

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 317    Accepted Submission(s): 166


Problem 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
 
 
本题在比赛的时候没有想到好的方法,索性没做,赛后相结合KMP算法运用动态规划思想,不了能力太弱,也没能动手,结果就写了一个5重循环,竟能过
后来索性提交了几次,得到了一些无关技巧的小技巧,裸函数、宏定义、内联函数对运行时间的影响
#include<iostream>
#include<cstdio>
using namespace std;
//#define Min(a,b) a<b?a:b
//#define Max(a,b) a>b?a:b

const int MAX=300+10;
int da[MAX][MAX];

//宏定义Max(a,b),Min(a,b),运行时间为512Ms
//直接写裸函数int Min(int a,int b),int Max(int a,int b),运行时间为656Ms
//写内联函数inline int Min(int a,int b),inline int Max(int a,int b),运行时间为525Ms
int Min(int a,int b)
{
	return a<b?a:b;
}
int Max(int a,int b)
{
	return a<b?b:a;
}

inline int Judge(int x,int y,int len)
{
	int i,j,k;
	for(k=y;k<=y+len-1;k++)
	{
		for(i=x,j=x+len-1;i<=j;i++,j--)
		{
			if(da[i][k]!=da[j][k])
				return 0;
		}
	}
	
	for(k=x;k<=x+len-1;k++)
	{
		for(i=y,j=y+len-1;i<=j;i++,j--)
		{
			if(da[k][i]!=da[k][j])
				return 0;
		}
	}
	return len;
}


int main()
{
	int cas,i,j,n,m,ans,len,tmp;
	cin>>cas;
	while(cas--)
	{
		scanf("%d%d",&n,&m);
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
				scanf("%d",&da[i][j]);
		}
		
		ans=0;
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				len=Min(n-i,m-j)+1;
				do
				{
					tmp=Judge(i,j,len);
					ans=Max(ans,tmp);
				}while(--len);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

你可能感兴趣的:(模拟)