hdu 4495 - Rectangle(hash+二分+dp)

Rectangle
Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 344 Accepted Submission(s): 100

Problem Description
Given a rectangle which contains N rows and each of them contains a string length of M.
You must find out a symmetrical isosceles right triangle (with two equal edges and a right angle) with two edges parallel two side of the rectangle. Symmetry means the value should be the same according to the shortest altitude of the triangle. And just output the area of the triangle.

Input
The first line of the input contains an integer T (1 <= T <= 20), which mean there are T test case follow.
For each test case, the first line contains two integer number N and M (1 <= N, M <= 500) which means described above.
And then N lines follow, which contains a string of length M. The string only contains letters or digits.

Output
For each test case, output a single integer that is the answer to the problem described above.

Sample Input

1
4 4
abab
dacb
adab
cabb

Sample Output

6

下面思路转自:这里写链接内容
腰平行于矩阵的边,其实也就是做四个方向,这里我的处理方法是只做一个方向的解,然后把矩阵旋转三次,分别求solution。做法是这样的,先预处理出一个数组,这里我将其记为edge[i][j],表示以(i,j)为顶点的,使得横向和纵向两条边相同的最长长度。这里就用到二分+hash了。每一行,每一列分别hash。处理完了之后,从上往下dp下来,dp[i][j]表示以(i.j)为顶点的对称等腰直角三角形的腰最长为多少。那么dp[i][j] = min ( edge[i][j] , dp[i-1][j-1] + 2 )。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef unsigned long long LL;

const int maxn=510;
const int B=123;
LL H[maxn][maxn][2],xp[maxn];
char s[maxn][maxn];
int N,M;
int dp[maxn][maxn];
int edge[maxn][maxn];
LL cal(int x,int l,int r,int c)
{
    return H[x][r][c]-H[x][l][c]*xp[r-l];
}
bool judge(int x,int y,int len)
{
    LL a=cal(x,y-len,y,0);
    LL b=cal(y,x-len, x, 1);
    return a==b;
}
int solve(int n,int m)
{
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            H[i][j][0]=H[i][j-1][0]*B+s[i][j];
    for(int j=1;j<=m;j++)
        for(int i=1;i<=n;i++)
            H[j][i][1]=H[j][i-1][1]*B+s[i][j];
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int l=0,r=min(i,j);
            while(l<=r)
            {
                int mid=(l+r)>>1;
                if(judge(i,j,mid))l=mid+1;
                else r=mid-1;
            }
            edge[i][j]=l-1;
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            dp[i][j]=min(edge[i][j],dp[i-1][j-1]+2),
            ans=max(ans,dp[i][j]);
    return (ans+1)*ans/2;
}
char s1[maxn][maxn];
void shuffle(int &n,int &m)
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            s1[i][j]=s[i][j];
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
            s[i][j]=s1[j][m-i+1];
    swap(n,m);
}
int main()
{
    int T;
    scanf("%d",&T);
    xp[0]=1;
    for(int i=1;i<maxn;i++)xp[i]=xp[i-1]*B;
    while(T--)
    {
        scanf("%d%d",&N,&M);
        for(int i=1;i<=N;i++)
            scanf("%s",s[i]+1);
        int ans=0;
        for(int i=1;i<=4;i++)
        {
            ans=max(solve(N,M),ans);
            shuffle(N,M);
        }
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值