原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=2870
一:原题内容
Problem 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
二:分析理解
这题用栈的方法竟然超时了,可能是stack的内存分配时间大了,只能改成比较麻烦的定义两个数组,left和right。。。。。。
这题是hdu1506-->http://blog.csdn.net/laojiu_/article/details/50954873
和hdu1505--> http://blog.csdn.net/laojiu_/article/details/50955681的进阶版
三:AC代码
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char ch[1005][1005];
int a[1005][1005];
int b[1005][1005];
int c[1005][1005];
int l[1005];
int r[1005];
int n, m;
int Solve(int x[1005][1005])
{
int finalAns = -1;
for (int i = 1; i <= n; i++)
{
l[1] = 1;
r[m] = m;
for (int j = 2; j <= m; j++)
{
l[j] = j;
while (l[j] != 1 && x[i][j] <= x[i][l[j] - 1])
l[j] = l[l[j] - 1];
}
for (int j = m - 1; j >= 1; j--)
{
r[j] = j;
while (r[j] != m&&x[i][j] <= x[i][r[j] + 1])
r[j] = r[r[j] + 1];
}
for (int j = 1; j <= m; j++)
if (x[i][j] * (r[j] - l[j] + 1) > finalAns)
finalAns = x[i][j] * (r[j] - l[j] + 1);
}
return finalAns;
}
int main()
{
char ch;
while (~scanf("%d%d", &n, &m))
{
for (int i = 1; i <= n; i++)
{
getchar();
for (int j = 1; j <= m; j++)
{
scanf("%c", &ch);
//a
if (ch == 'a' || ch == 'w' || ch == 'y' || ch == 'z')
a[i][j] = a[i - 1][j] + 1;
else
a[i][j] = 0;
//b
if (ch == 'b' || ch == 'w' || ch == 'x' || ch == 'z')
b[i][j] = b[i - 1][j] + 1;
else
b[i][j] = 0;
//c
if (ch == 'c' || ch == 'x' || ch == 'y' || ch == 'z')
c[i][j] = c[i - 1][j] + 1;
else
c[i][j] = 0;
}
}
int a_ans = Solve(a);
int b_ans = Solve(b);
int c_ans = Solve(c);
printf("%d\n", max(max(a_ans, b_ans), c_ans));
}
return 0;
}