题目描述
Every morning when they are milked, theFarmer John's cows form a rectangular grid that is R (1 <= R <= 10,000)rows by C (1 <= C <= 75) columns. As we all know, Farmer John is quitethe expert on cow behavior, and is currently writing a book about feedingbehavior in cows. He notices that if each cow is labeled with an uppercaseletter indicating its breed, the two-dimensional pattern formed by his cowsduring milking sometimes seems to be made from smaller repeating rectangularpatterns. Help FJ find the rectangular unit of smallest area that can berepetitively tiled to make up the entire milking grid. Note that the dimensionsof the small rectangular unit do not necessarily need to divide evenly thedimensions of the entire milking grid, as indicated in the sample input below.
输入
* Line 1: Two space-separated integers: Rand C * Lines 2..R+1: The grid that the cows form, with an uppercase letterdenoting each cow's breed. Each of the R input lines has C characters with nospace or other intervening character.
输出
* Line 1: The area of the smallest unitfrom which the grid is formed
样例输入
2 5
ABABA
ABABA
样例输出
2
解题思路:
这道题目要有条理性,算完行可以满足的最大值,再算列可以满足的最大值,从而达到数量最小的目的。
当然,我们只需要两个分别计算行列的KMP中两个不同的next数组,答案即是:(m-hnext[m])* (n-snext[n])。
代码:(请不要直接拷贝哦)
#include<cstdio>
#include<cstring>
int n,m,hnext[80],snext[10005],h,l;
char st[10005][80];
inline bool hpd(int x,int y)//计算行的next数组计算
{
int i;
for(i=0;i<n;i++)
if(st[i][x]!=st[i][y]) return 0;//暴力,判断当前区间是否全部相等。
return 1;
}
inline bool spd(int x,int y)//计算列的next数组计算
{
int i;
for(i=0;i<m;i++)
if(st[x][i]!=st[y][i]) return 0;
return 1;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=0;i<n;i++) scanf("%s",st[i]);
int i=0,j=-1;
hnext[0]=-1;
while (i<m)
if((j==-1)||(hpd(i,j))) hnext[++i]=++j;
else j=hnext[j];
i=0,j=-1;
snext[0]=-1;
while (i<n)
if((j==-1)||(spd(i,j))) snext[++i]=++j;
else j=snext[j];
l=m-hnext[m];//算出列的满足答案的最大值
h=n-snext[n];//算出行的满足答案的最大值
printf("%d\n",h*l);
return 0;
}