Description
Every morning when they are milked, the Farmer 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 quite the expert on cow behavior, and is currently writing a book about feeding behavior in cows. He notices that if each cow is labeled with an uppercase letter indicating its breed, the two-dimensional pattern formed by his cows during milking sometimes seems to be made from smaller repeating rectangular patterns.
Help FJ find the rectangular unit of smallest area that can be repetitively tiled to make up the entire milking grid. Note that the dimensions of the small rectangular unit do not necessarily need to divide evenly the dimensions of the entire milking grid, as indicated in the sample input below.
Input
* Line 1: Two space-separated integers: R and C
* Lines 2..R+1: The grid that the cows form, with an uppercase letter denoting each cow's breed. Each of the R input lines has C characters with no space or other intervening character.
Output
* Line 1: The area of the smallest unit from which the grid is formed
Sample Input
2 5
ABABA
ABABA
Sample Output
2
Hint
The entire milking grid can be constructed from repetitions of the pattern 'AB'.
Source
【分析】
一开始还想错了,
其实是理解题意错了。
直接枚举每行的重复长度,然后找到一个使得每行都可以重复的长度。
竖行HASH+KMP
/*
唐代李白
《登金陵凤凰台》 凤凰台上凤凰游,凤去台空江自流。
吴宫花草埋幽径,晋代衣冠成古丘。
三山半落青天外,二水中分白鹭洲。
总为浮云能蔽日,长安不见使人愁
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int MAXM = + ;
const int INF = ;
const int SIZE = ;
const int maxnode = 0x7fffffff + ;
using namespace std;
int l1, l2;
int next[MAXN];//不用开太大了..
int Ans[MAXN];
char data[MAXN][MAXM];
int r, c;//长和宽 /*void getNext(){
next[1] = 0;
int j = 0;
for (int i = 2; i <= n; i++){
while (next[j] > 0 && strcmp(data[j + 1] + 1, data[i] + 1)) j = next[j];
if (!strcmp(data[j + 1] + 1, data[i] + 1)) j++;
next[i] = j;
}
return;
}*/
//a是模板链, b是匹配串
/*int kmp(char *a, char *b){
int j = 0, cnt = 0;
for (int i = 1; i <= l2; i++){
while (next[j] > 0 && a[j + 1] == b[i]) j = next[j];
if (a[j + 1] == b[i]) j++;
if (j == m) return 1;//?
}
}*/ /*void init(){
scanf("%s", a + 1);
scanf("%s", b + 1);
l1 = strlen(a + 1);
l2 = strlen(b + 1);
}*/
int cnt[MAXM], h[MAXN];
char a[MAXM]; void init(){
scanf("%d%d", &r, &c);
memset(cnt, , sizeof(cnt));
for (int i = ; i < r; i++){
scanf("%s", data[i]);
strcpy(a, data[i]);
for(int j = c - ; j > ; j--){
a[j]=;
int x = , y;
for(y = ; data[i][y] ; y++){
if(!a[x]) x = ;
if(a[x] != data[i][y]) break;
x++;
}
if( !data[i][y] ) cnt[j]++;
}
}
}
void work(){
int i;
for(i = ; i < c; i++) if(cnt[i] == r)break;
int x = i;
for(int i = ;i < r; i++) data[i][x] = ;
next[] = -;//按纵列求KMP的next函数,以求最小重复子矩阵的行数
for(int i = , j = -; i < r; i++){
while(j != - && strcmp(data[j+],data[i])) j = next[j];
if(!strcmp(data[j+], data[i])) j++;
next[i] = j;
}
printf("%d\n",(r - - next[r-]) * x);//行列相乘即为最终结果
} int main(){
int T; init();
work(); return ;
}