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
相关文章
相关标签/搜索