题链:https://nanti.jisuanke.com/t/42391
题意:如题目名字,求两个矩阵的最大公共子矩阵。
思路:首先,预处理出每个点上边有多少相同的以及每个点右边有多少相同的。枚举每行,然后以求出的右边相同的个数为依据,划分左右可以连的那几列,那么问题就变成了(POJ - 3494)单调栈模板题,直接求最大的面积即可。
(太菜了太菜了,想了两种方法都不对(有一个错在倒数第3个测试点,哎。。。。)。)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3+10;
const int M = 1e6+10;
int n,m;
int a[N][N],b[N][N];
int h[N][N],w[N][N];
struct node
{
int x,y;
}pos[M];
bool sameup(int x,int y){
return pos[x].x+1==pos[y].x&&pos[x].y==pos[y].y;
}
bool sameright(int x,int y){
return pos[x].x==pos[y].x&&pos[x].y+1==pos[y].y;
}
int l[N],r[N],L,R;
int top,sta[N];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]);
for(int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
scanf("%d",&b[i][j]);
pos[b[i][j]].x=i;
pos[b[i][j]].y=j;
}
int ans=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(i==1) h[i][j]=1;
else{
if(sameup(a[i-1][j],a[i][j]))
h[i][j]=h[i-1][j]+1;
else h[i][j]=1;
}
}
}
for(int i=1;i<=n;i++){
for(int j=m;j>=1;j--){
if(j==m) w[i][j]=1;
else{
if(sameright(a[i][j],a[i][j+1]))
w[i][j]=w[i][j+1]+1;
else w[i][j]=1;
}
}
}
for(int i=1;i<=n;i++){
int col=1;
while(col<=m){
top=0;
//[L,R]是可以横着相连的那几列
L=col,R=col+w[i][col]-1;
for(int j=L;j<=R;j++){
while(top&&h[i][sta[top]]>=h[i][j]) top--;
if(!top) l[j]=L;
else l[j]=sta[top]+1;
sta[++top]=j;
}
top=0;
for(int j=R;j>=L;j--){
while(top&&h[i][sta[top]]>=h[i][j]) top--;
if(!top) r[j]=R;
else r[j]=sta[top]-1;
sta[++top]=j;
}
for(int j=L;j<=R;j++)
ans=max(ans,h[i][j]*(r[j]-l[j]+1));
col+=w[i][col];
}
}
printf("%d\n",ans);
}
错误思路:将一行一列的单独拿出来考虑。想在考虑至少去两行两列的,如果首先上下必须一样,然后左右必须也一样;那么对于每一个点只有他的上方与右方都一样才为1,否则就为0。然后问题就变成上面提到的单调栈模板题,最后长和宽都要加1,因为每行每列取得数要加1。
(哪位大佬可以帮忙看看。。。。。。。。。。。。。。。。。。)
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 1e3+10;
const int M = 1e6+10;
int n,m;
int a[N][N],b[N][N];
int c[N][N],c1[N][N];
int h[N],w[N];
struct node
{
int x,y;
}pos[M];
bool sameup(int x,int y){
return pos[x].x+1==pos[y].x&&pos[x].y==pos[y].y;
}
bool sameright(int x,int y){
return pos[x].x==pos[y].x&&pos[x].y+1==pos[y].y;
}
int top,sta[N],l[N],r[N];
void getlr(){
top=0;
for(int i=1;i<m;i++){
while(top&&h[sta[top]]>=h[i]) --top;
if(top==0) l[i]=1;
else l[i]=sta[top]+1;
sta[++top]=i;
}
top=0;
for(int i=m-1;i>=1;i--){
while(top&&h[sta[top]]>=h[i]) --top;
if(top==0) r[i]=m-1;
else r[i]=sta[top]-1;
sta[++top]=i;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]);
for(int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
scanf("%d",&b[i][j]);
pos[b[i][j]].x=i;
pos[b[i][j]].y=j;
}
int ans=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(i>1){
if(sameup(a[i-1][j],a[i][j]))
h[j]=h[j]+1;
else h[j]=0;
if(h[j]+1>ans) ans=h[j]+1;
}
if(j<m){
if(sameright(a[i][j],a[i][j+1]))
w[j]=w[j-1]+1;
else w[j]=0;
if(w[j]+1>ans) ans=w[j]+1;
}
}
}
for(int i=1;i<=m;i++){
h[i]=0;
}
for(int i=2;i<=n;i++){
for(int j=1;j<=m;j++){
if(sameup(a[i-1][j],a[i][j])) c[i][j]=1;
else c[i][j]=0;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<m;j++){
c[i][j]=c[i][j]&&sameright(a[i][j],a[i][j+1]);
}
}
for(int i=2;i<=n;i++){
for(int j=1;j<m;j++)
h[j]=c[i][j]?h[j]+1:0;
getlr();
for(int j=1;j<m;j++){
ans=max(ans,(h[j]+1)*(r[j]-l[j]+2));
}
}
printf("%d\n",ans);
}