poj2185Milking Grid

Milking Grid
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8325   Accepted: 3588

Descriptionide

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.

Inputui

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

Outputspa

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

Sample Inputcode

2 5
ABABA
ABABA

Sample Outputorm

2

Hintblog

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

Sourceip

___________________________________________________________________________________________________________________字符串

題目大意:給定矩形字符矩陣,問它是多大的子矩陣鋪成的。get

每一行(或列)組成的字符串,最後一個字母的next[]爲它的前綴,那麼「最後一個字母座標-next[最後一個字母]」就是當前行(或列)鋪設的字符串的長度(標記爲L)。那麼全部行(或列)的L的最小公倍數就是最終子矩陣的長(或寬)。長寬相乘就是結果。須要注意的是當子矩陣長(或寬)大於矩陣自己的長(或寬)時,則直接賦值爲矩陣自己的長(或寬),也就是說結果最大也就是矩陣自己。input

___________________________________________________________________________________________________________________

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 int r,c;
 8 int rg=1,cg=1;
 9 char str[10010][77];
10 char s[10010];
11 int next[10010];
12 
13 void getnext(int c)
14 {
15     next[0]=-1;
16     for(int j,i=1;i<c;i++)
17     {
18         j=next[i-1];
19         while(s[j+1]!=s[i] && j>=0)j=next[j];
20         next[i]=s[j+1]==s[i]?j+1:-1;
21     }
22 }
23 int gys(int a,int b)
24 {
25     if(a%b==0)return b;
26     else return gys(b,a%b);
27 }
28 int gbs(int a,int b)
29 {
30     return a/gys(a,b)*b;
31 }
32 int main()
33 {
34     scanf("%d%d",&r,&c);
35     for(int i=0;i<r;i++)
36     {
37         scanf("%s",str[i]);
38         strcpy(s,str[i]);
39         getnext(c);
40         rg=gbs(rg,c-next[c-1]-1);
41         if(rg>c)rg=c;
42     }
43     s[r]=0;
44     for(int i=0;i<c;i++)
45     {
46         for(int j=0;j<r;j++)s[j]=str[j][i];
47         getnext(r);
48         cg=gbs(cg,r-next[r-1]-1);
49         if(cg>r)cg=r;
50     }
51     printf("%d",rg*cg);
52     return 0;
53 }
View Code
相關文章
相關標籤/搜索