Milking Grid poj2185

Milking Gridios

POJ - 2185
時限: 3000MS   內存: 65536KB   64位IO格式: %I64d & %I64u

 狀態ide

已開啓劃詞翻譯

問題描述ui

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. 

輸入spa

* 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. 

輸出翻譯

* Line 1: The area of the smallest unit from which the grid is formed 

樣例輸入code

2 5
ABABA
ABABA

樣例輸出orm

2

提示blog

The entire milking grid can be constructed from repetitions of the pattern 'AB'.

來源ip

kmp延伸……你是豬麼
 
作兩次KMP
行和列分別是len-next[len];
最後兩個結果相乘就能夠了
 
本題只是把線性變成平面,kmp對字符的操做變成字符串……
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 #define maxn 1000008
 8 
 9 char s[maxn][80];
10 int r, c, next[maxn];
11 
12 bool same1(int i, int j)   // 判斷第i行和第j行是否相等
13 {
14     for(int k = 0; k < c; k++)
15         if(s[i][k] != s[j][k])
16             return false;
17     return true;
18 }
19 
20 bool same2(int i, int j)  // 判斷第i列和第j列是否相等。
21 {
22     for(int k = 0; k < r; k++)
23         if(s[k][i] != s[k][j])
24             return false;
25     return true;
26 }
27 
28 int main()
29 {
30     while(~scanf("%d%d", &r, &c))
31     {
32         for(int i = 0; i < r; i++)
33             scanf("%s", s[i]);
34         int j, k;
35         memset(next, 0, sizeof(next));
36         j = 0;
37         k = next[0] = -1;
38         while(j < r)
39         {
40             while(-1 != k && !same1(j, k))
41                 k = next[k];
42             next[++j] = ++k;
43         }
44         int ans1 = r - next[r];     //  r-next[r]就是須要的最短的長度能夠覆蓋這個平面
45         memset(next, 0, sizeof(next));
46         j = 0;
47         k = next[0] = -1;
48         while(j < c)
49         {
50             while(-1 != k && !same2(j, k))
51                 k = next[k];
52             next[++j] = ++k;
53         }
54         int ans2 = c - next[c];  //列的 
55 
56         printf("%d\n", ans1*ans2);
57     }
58     return 0;
59 }
相關文章
相關標籤/搜索