zoj 3367 Counterfeit Money(dp)

先搞定这题。ZOJ1985 Largest Rectangle in a Histogram

再做这题。先枚举第二个矩形对第一个矩形的偏移量(x,y),再进行2维DP,复杂度为O(n^2 *n^2),即O(n^4).

#include <bits/stdc++.h>
using namespace std;
const int maxn=42;
int n1,m1,n2,m2;
char a[maxn][maxn];
char b[maxn][maxn];
int dp[maxn][maxn];
int L[maxn],R[maxn];
int mx,u,v,w,z,rr,cc;
bool inmaze(int i,int j){
    return i>=0&&i<n2&&j>=0&&j<m2;
}
void DP(int r,int c){
    memset(dp,0,sizeof dp);
    for(int i=0;i<n1;i++)
    for(int j=0;j<m1;j++){
        if(inmaze(i+r,j+c)){
            if(a[i][j]!=b[i+r][j+c])continue;
            if(i)dp[i][j]=dp[i-1][j]+1;
            else dp[i][j]=1;
        }
    }
    for(int i=0;i<n1;i++){
        for(int j=0;j<m1;j++)L[j]=R[j]=j;
        for(int j=1;j<m1;j++){
            int t=L[j];
            while(t&&dp[i][t-1]>=dp[i][j])t=L[t-1];
            L[j]=t;
        }
        for(int j=m1-2;j>=0;j--){
            int t=R[j];
            while(t<m1-1&&dp[i][t+1]>=dp[i][j])t=R[t+1];
            R[j]=t;
        }
        for(int j=0;j<m1;j++){
            if(!dp[i][j])continue;
            int res=dp[i][j]*(R[j]-L[j]+1);
            if(res>mx)mx=res,rr=dp[i][j],cc=R[j]-L[j]+1,v=L[j],u=i-dp[i][j]+1,w=u+r,z=v+c;
        }
    }
}
void run(){
    for(int i=0;i<n1;i++)scanf("%s",a[i]);
    scanf("%d%d",&n2,&m2);
    for(int i=0;i<n2;i++)scanf("%s",b[i]);
    mx=0;
    for(int i=1-n1;i<n2;i++)
    for(int j=1-m1;j<m2;j++){
        DP(i,j);
    }
    if(!mx)puts("0 0");
    else printf("%d %d\n%d %d\n%d %d\n",rr,cc,u+1,v+1,w+1,z+1);
}
int main()
{
//    freopen("in","r",stdin);
    while(scanf("%d%d",&n1,&m1)>0)run();
    return 0;
}

  

时间: 2024-12-17 23:09:56

zoj 3367 Counterfeit Money(dp)的相关文章

[ACM] ZOJ 3725 Painting Storages (DP计数+组合)

Painting Storages Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to paint all storages with two colors: red and blue. Each storage will be painted with e

ZOJ 3306 状压dp

转自:http://blog.csdn.net/a497406594/article/details/38442893 Kill the Monsters Time Limit: 7 Seconds Memory Limit: 32768 KB In order to celebrate the 8th anniversary of ZOJ, watashi introduces a strange game to other ZJU ACM team members. The board of

Food Delivery ZOJ - 3469 (区间dp)

Food Delivery ZOJ - 3469 题意:快递员送外卖,n个客户,起始位置为x,速度为v,每个客户单位时间不满意度增加hi,问最少增加多少不满意度. 每一个客户可能是从左侧送到或者从右侧送到. 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define CLR(m,a) memset(m,a,sizeof(m)) 4 const int maxn=1010; 5 const int inf=0x3f3f3f3f; 6 i

zoj 3634 Bounty hunter(dp,没完全想清楚,需要回头再看_20151027)

M - Bounty hunter Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description Bounty hunter is a hero who always moves along cities to earn money by his power. One day he decides to N cities one by one At the

zoj 3471状态压缩DP

#include<stdio.h> #include<string.h> int max(int a,int b) { if(a>b) return a; return b; } int dp[100000],map[15][15],mark[15]; int main() { int i,j,n,m,k; while(scanf("%d",&n)!=EOF&&n) { for(i=1;i<=n;i++) for(j=1;j&

ZOJ 3822 Domination 概率DP

题意:在一个n*m的棋盘上放棋子,一个棋子可覆盖一行一列,若n行m列全被覆盖则停止放棋子,求棋子的期望 思路:期望DP, dp[i][j][k]表示放了i个棋子覆盖了j行k列 已知dp[0][0][0]=1,求dp[1~n*m][n][m] 四种情况: 1.再放一个棋子,行列都不增加 dp[i+1][j][k]+=dp[i][j][k]*(j*k-i)*1.0/(m*n-i); 2.只增加一行 dp[i+1][j+1][k]+=dp[i][j][k]*(n-j)*k*1.0/(m*n-i); 3

ZOJ 3329 【概率DP】

题意: 给你三个均匀k面筛子. 分别有k1 k2 k3个面,每个面朝上的概率是相等的. 如果第一个筛子出现a第二个筛子出现b第三个筛子出现c那么置零. 否则在当前和加上三个点数之和. 求当前和大于n需要的步数的期望. 思路: 一开始状态转移搞错了,手推公式交了WA,后来想了想状态转移的过程发现每个状态都跟0状态有关系,但是dp[0]不确定,但是幸运的是这是一个线性变换,所以状态转移的时候记录一下dp[0]的系数,最后移项输出就好了. dp[i]=dp[i+x]*(k1*k2*k3);(x=i+j

zoj 3471(状态压缩DP,类似于点集配对)

Most Powerful Time Limit: 2 Seconds      Memory Limit: 65536 KB Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a

zoj 3623 Battle Ships dp

 Description Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N kinds of battle ships. The factory takes ti second