【DP】 HDU 2859 Phalanx
2015-02-21 14:34
302 查看
求在对角线上对称的最大子矩阵
以右上角为上一个状态
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <deque>
#include <map>
#define cler(arr, val) memset(arr, val, sizeof(arr))
typedef long long LL;
const int MAXN = 1000100;
const int MAXM = 20000;
const int INF = 0x3f3f3f3f;
const LL mod = 10000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
char s[1022][1022];
int dp[1011][1011];
int main()
{
int n;
while(cin>>n,n)
{
for(int i=1;i<=n;i++)
cin>>s[i]+1;
cler(dp,0);
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==1||j==n)
dp[i][j]=1;
int x=i,y=j;
while(x>=1&&y<=n&&s[x][j]==s[i][y])
{
x--,y++;
}
if(i-x>=dp[i-1][j+1]+1)
dp[i][j]=dp[i-1][j+1]+1;
else dp[i][j]=i-x;
ans=max(ans,dp[i][j]);
}
}
cout<<ans<<endl;
}
return 0;
}
以右上角为上一个状态
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <deque>
#include <map>
#define cler(arr, val) memset(arr, val, sizeof(arr))
typedef long long LL;
const int MAXN = 1000100;
const int MAXM = 20000;
const int INF = 0x3f3f3f3f;
const LL mod = 10000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
char s[1022][1022];
int dp[1011][1011];
int main()
{
int n;
while(cin>>n,n)
{
for(int i=1;i<=n;i++)
cin>>s[i]+1;
cler(dp,0);
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==1||j==n)
dp[i][j]=1;
int x=i,y=j;
while(x>=1&&y<=n&&s[x][j]==s[i][y])
{
x--,y++;
}
if(i-x>=dp[i-1][j+1]+1)
dp[i][j]=dp[i-1][j+1]+1;
else dp[i][j]=i-x;
ans=max(ans,dp[i][j]);
}
}
cout<<ans<<endl;
}
return 0;
}
相关文章推荐
- HDU 2859 Phalanx(dp吧~ ~ ~)
- 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】
- 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
- 【HDU】2859 Phalanx (DP)
- HDU 2859 Phalanx 简单DP
- 【dp】HDU 2859 Phalanx