HDU 2859 Phalanx (DP)
2017-07-26 12:04
399 查看
题目链接:hdu2859
题目大意:给出一个矩阵 ,输出最大对称子矩阵的大小
矩阵如:
cbx
cpb
zcc 为对称矩阵 ,即以右对角线为对称轴
思路:dp题,对每个点进行探索。首先大的矩阵的最上边和最右边是边界,所有值为1,其他的点对他的上和右探索最多连续多少个对应相同,每个点与其右上方的那个点组成对称矩阵,大小取决于较小的那个。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
int dp[1010][1010];
char str[1010][1010];
int main()
{
int n;
while(scanf("%d",&n) == 1 && n)
{
for(int i = 0;i < n;i++)
scanf("%s",str[i]);
int ans = 1;
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
{
if(i == 0 || j == n-1)
{
dp[i][j] = 1;
continue;
}
int t1 = i, t2 = j;
while(t1 >= 0 && t2 < n && str[t1][j] == str[i][t2])//探索的他的上与右之多连续相同多少个
{
t1--;
t2++;
}
t1 = i - t1;
if(t1 >= dp[i-1][j+1]+1)dp[i][j] = dp[i-1][j+1]+1;//与前一个位置组成矩阵 取小的
else dp[i][j] = t1;
ans = max(ans,dp[i][j]);
}
printf("%d\n",ans);
}
return 0;
}
题目大意:给出一个矩阵 ,输出最大对称子矩阵的大小
矩阵如:
cbx
cpb
zcc 为对称矩阵 ,即以右对角线为对称轴
思路:dp题,对每个点进行探索。首先大的矩阵的最上边和最右边是边界,所有值为1,其他的点对他的上和右探索最多连续多少个对应相同,每个点与其右上方的那个点组成对称矩阵,大小取决于较小的那个。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
int dp[1010][1010];
char str[1010][1010];
int main()
{
int n;
while(scanf("%d",&n) == 1 && n)
{
for(int i = 0;i < n;i++)
scanf("%s",str[i]);
int ans = 1;
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
{
if(i == 0 || j == n-1)
{
dp[i][j] = 1;
continue;
}
int t1 = i, t2 = j;
while(t1 >= 0 && t2 < n && str[t1][j] == str[i][t2])//探索的他的上与右之多连续相同多少个
{
t1--;
t2++;
}
t1 = i - t1;
if(t1 >= dp[i-1][j+1]+1)dp[i][j] = dp[i-1][j+1]+1;//与前一个位置组成矩阵 取小的
else dp[i][j] = t1;
ans = max(ans,dp[i][j]);
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- HDU 2859 Phalanx(DP)
- hdu-2859 Phalanx 【dp】
- HDU 2859 Phalanx(二维DP)
- 简单dp,HDU 2859 Phalanx
- HDU - 2859 Phalanx (DP)
- 【HDU】2859 Phalanx (DP)
- hdu(2859)——Phalanx(dp)
- HDU 2859 Phalanx——DP
- 【dp】 HDU - 2859 Phalanx
- DP-HDU-2859-Phalanx
- HDU 2859 Phalanx 简单DP
- 【DP】 HDU 2859 Phalanx
- HDU 2859 Phalanx(DP)
- HDU 2859 Phalanx (dp)
- hdu(2859)——Phalanx(dp)
- HDU 2859 Phalanx (DP)
- 【dp】HDU 2859 Phalanx
- HDU 2859 Phalanx (DP)
- (HDU 2859) Phalanx (求最大对称子矩阵) dp
- Hdu 2859 - Phalanx (基础dp)