上原题
Description
Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change 'w' to 'a' or 'b', change 'x' to 'b' or 'c', change 'y' to 'a' or 'c', and change 'z' to 'a', 'b' or 'c'. After you changed it, what's the largest submatrix with the same letters you can make?
Input
The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 1000) on line. Then come the elements of a matrix in row-major order on m lines each with n letters. The input ends once EOF is met.
Output
For each test case, output one line containing the number of elements of the largest submatrix of all same letters.
Sample Input
2 4
abcw
wxyz
Sample Output
3
把所有能转化的全部依次转化为'a','b','c',用解第二题的方法对每种情况用一次就行了。。
//Li Wenjun
//emai:1542113545@qq.com
/*
「“04.24,サクラと东京スカイツリーに行った。そこは世界で一番暖かいところだ。”
“04.26,サクラと明治神宫に行った。そこで结婚式お挙げる人がいた。”
“04.25,サクラとデイズニーに行った。お化け屋敷が怖かったけど、サクラがいたから、全然怖くわなかった。”
“サクラのことが大好き。”」
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
#include <algorithm>
using namespace std;
int dp[1010][1010];
char a[1010][1010];
int r[1005],l[1005];
int m,n,maxs;
int FindMax(int H)
{
int temp,i,j;
for(i=1;i<=m;i++)
{
r[i]=l[i]=i;
while(dp[H][l[i]-1]>=dp[H][i])
l[i]=l[l[i]-1];
}
for(j=m;j>=1;j--)
{
while(dp[H][r[j]+1]>=dp[H][j])
r[j]=r[r[j]+1];
temp=dp[H][j]*(r[j]-l[j]+1);
if(maxs<temp)
maxs=temp;
}
return 0;
}
int dps(char x1,char x2,char x3,char x4)
{
/* cout<<x1<<' ';
cout<<x2<<' ';
cout<<x3<<' ';
cout<<x4<<' ';
cout<<endl; */
memset(dp,0,sizeof(dp));
memset(r,0,sizeof(r));
memset(l,0,sizeof(l));
dp[n+1][0]=-2;
for(int i=1;i<=n;i++)
{
dp[i][0]=-1;
for(int j=1;j<=m;j++)
{
// cout<<a[i][j]<<' ';
if(a[i][j]==x1||a[i][j]==x2||a[i][j]==x3||a[i][j]==x4)
{
dp[i][j]=dp[i-1][j]+1;
}
else
{
dp[i][0]=-2;
dp[i][j]=0;
}
}
dp[i][m+1]=-1;
// cout<<endl;
}
/* for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout<<dp[i][j]<<' ';
}
cout<<endl;
}
*/
for(int i=1;i<=n;i++)
{
if(dp[i+1][0]==-2)
FindMax(i);
}
// cout<<maxs<<endl;
return 0;
}
int main()
{
cin.tie(0);
cin.sync_with_stdio(false);
// freopen("in.txt","r",stdin);
while(cin>>n>>m)
{
memset(dp,0,sizeof(dp));
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{
cin>>a[i]+1;
}
maxs=0;
dps('a','w','y','z');
dps('b','w','x','z');
dps('c','x','y','z');
cout<<maxs<<endl;
}
}
本文介绍了一种算法,用于解决给定特定字符转换规则的矩阵中寻找最大的完全由相同字母组成的子矩阵的问题。通过将可转换字符统一为目标字符并运用特定算法,有效地找出最大相同子矩阵。
2841

被折叠的 条评论
为什么被折叠?



