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.Input
* Line 1: Two space-separated integers: R and COutput
* Line 1: The area of the smallest unit from which the grid is formedSample Input
2 5 ABABA ABABA
Sample Output
2
Hint
The entire milking grid can be constructed from repetitions of the pattern 'AB'.Source
USACO 2003 Fall#include#include char s[10005][80], rs[80][10005]; int R[10005], C[10005]; int r, c; void get_nextR() { R[0] = -1; int j = -1, i = 0; while(i < r) { if(j == -1 || strcmp(s[i], s[j]) == 0) { i++; j++; R[i] = j; } else j = R[j]; } } void get_nextC() { C[0] = -1; int j = -1, i = 0; while(i < c) { if(j == -1 || strcmp(rs[i], rs[j]) == 0) { i++; j++; C[i] = j; } else j = C[j]; } } int main() { while(scanf("%d %d", &r, &c) != EOF) { for(int i = 0; i < r; i++) scanf("%s", s[i]); get_nextR(); for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) rs[j][i] = s[i][j]; get_nextC(); printf("%d\n", (r - R[r]) * (c - C[c])); } }