【POJ2185】【KMP + HASH】Milking Grid

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
  1 /*

  2 唐代李白

  3 《登金陵凤凰台》

  4 

  5 凤凰台上凤凰游,凤去台空江自流。

  6 吴宫花草埋幽径,晋代衣冠成古丘。

  7 三山半落青天外,二水中分白鹭洲。

  8 总为浮云能蔽日,长安不见使人愁

  9 */

 10 #include <iostream>

 11 #include <cstdio>

 12 #include <algorithm>

 13 #include <cstring>

 14 #include <vector>

 15 #include <utility>

 16 #include <iomanip>

 17 #include <string>

 18 #include <cmath>

 19 #include <queue>

 20 #include <assert.h>

 21 #include <map>

 22 #include <ctime>

 23 #include <cstdlib>

 24 #include <stack>

 25 #define LOCAL

 26 const int MAXN = 10000 + 10;

 27 const int MAXM = 75 + 10;

 28 const int INF = 100000000;

 29 const int SIZE = 450;

 30 const int maxnode =  0x7fffffff + 10;

 31 using namespace std;

 32 int l1, l2;

 33 int next[MAXN];//不用开太大了.. 

 34 int Ans[MAXN];

 35 char data[MAXN][MAXM];

 36 int  r, c;//长和宽 

 37 

 38 /*void getNext(){

 39      next[1] = 0;

 40      int j = 0;

 41      for (int i = 2; i <= n; i++){

 42          while (next[j] > 0 && strcmp(data[j + 1] + 1, data[i] + 1)) j = next[j];

 43          if (!strcmp(data[j + 1] + 1, data[i] + 1)) j++;

 44          next[i] = j;

 45      }

 46      return;

 47 }*/

 48 //a是模板链, b是匹配串 

 49 /*int kmp(char *a, char *b){

 50      int j = 0, cnt = 0;

 51      for (int i = 1; i <= l2; i++){

 52          while (next[j] > 0 && a[j + 1] == b[i]) j = next[j];

 53          if (a[j + 1] == b[i]) j++;

 54          if (j == m) return 1;//?

 55      }

 56 }*/

 57 

 58 /*void init(){

 59      scanf("%s", a + 1);

 60      scanf("%s", b + 1);

 61      l1 = strlen(a + 1);

 62      l2 = strlen(b + 1);

 63 }*/

 64 int cnt[MAXM], h[MAXN];

 65 char a[MAXM];

 66 

 67 void init(){

 68      scanf("%d%d", &r, &c); 

 69      memset(cnt, 0, sizeof(cnt));

 70      for (int i = 0 ; i < r; i++){ 

 71         scanf("%s", data[i]); 

 72         strcpy(a, data[i]); 

 73         for(int j = c - 1; j > 0; j--){ 

 74             a[j]=0; 

 75             int x = 0, y;

 76             for(y = 0; data[i][y] ; y++){ 

 77                 if(!a[x]) x = 0; 

 78                 if(a[x] != data[i][y]) break; 

 79                 x++;

 80             } 

 81             if( !data[i][y] ) cnt[j]++; 

 82         } 

 83     } 

 84 }

 85 void work(){

 86     int i;

 87     for(i = 0; i < c; i++) if(cnt[i] == r)break; 

 88     int x = i;

 89     for(int i = 0;i < r; i++) data[i][x] = 0; 

 90     next[0] = -1;//按纵列求KMP的next函数,以求最小重复子矩阵的行数 

 91     for(int i = 1, j = -1; i < r; i++){ 

 92         while(j != -1 && strcmp(data[j+1],data[i])) j = next[j]; 

 93         if(!strcmp(data[j+1], data[i])) j++; 

 94         next[i] = j; 

 95     } 

 96     printf("%d\n",(r - 1 - next[r-1]) * x);//行列相乘即为最终结果 

 97 }

 98 

 99     

100 int main(){

101     int T;

102     

103     init();

104     work();

105     

106     return 0;

107 }
View Code

 

 

你可能感兴趣的:(hash)