Hello , code

# POJ 2185 (字符串最小覆盖)

Milking Grid
 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 3524 Accepted: 1446

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

#include<stdio.h>
#include<string.h>

#define clear(a) memset(a,0,sizeof(a))

char st[10010][80],s_temp[10010];
int r,c,p[10010];

int in_data()
{
int i;
scanf("%d%d",&r,&c);
for(i = 0;i < r;i++) scanf("%s",st[i]);
}

int gcd(int x,int y){
if(y==0) return x;
else return(gcd(y,x%y));
}

int lcm(int x,int y){
return x*y/gcd(x,y);
}

int get_next(char s[],int len)
{
int i,j,ans = len;
clear(p);
p[1] = 0;
j = 0;
for(i = 2;i <= len;i++) {
while ((j > 0) && (s[j+1] != s[i]))
j = p[j];
if (s[j+1] == s[i])
j++;
p[i] = j;
}
// printf("%d\n",ans);
return len - p[len];
}

int work()
{
int i,j,lcm_r = 1,lcm_c = 1;
for(i = 1;i <= r;i++) {
for(j = 1;j <= c;j++)
s_temp[j] = st[i-1][j-1];
s_temp[c+1] = '\0';
lcm_r = lcm(lcm_r,get_next(s_temp,c));
if (lcm_r >= c) {lcm_r = c;break;}
}
for(j = 1;j <= c;j++) {
for(i = 1;i <= r;i++)
s_temp[i] = st[i-1][j-1];
s_temp[r+1] = '\0';
lcm_c = lcm(lcm_c,get_next(s_temp,r));
if (lcm_c >= r) {lcm_c = r;break;}
}
//if (lcm_r > c) lcm_r = c;
//if (lcm_c > r) lcm_c = r;
return lcm_r * lcm_c;
}

int main()
{
in_data();
printf("%d\n",work());
return 0;
}


#### POJ2185 最小覆盖矩阵

2012-11-18 20:09:39

#### POJ 2185 Milking Grid(KMP:循环节加强版)

2014-04-05 17:24:37

#### poj3301--Texas Trip（最小正方形覆盖）

2015-08-06 19:14:01

#### POJ 2185(最大平铺矩阵）

2012-11-15 10:51:28

#### POJ 3020 Antenna Placement(无向图最小边覆盖)

2016-08-23 11:42:16

#### POJ 3617Best Cow Line（字典序最小的字符串）

2017-02-02 17:33:54

#### poj 2185 : Milking Grid (KMP)

2016-08-13 08:25:00

#### Lintcode 最小子串覆盖

2017-03-16 21:21:56

#### poj 1848 Tree(树形ＤＰ，太难了，三种状态，四种状态转换)

2014-01-21 17:28:07

#### POJ 1463 浅谈简单树形动态规划及树上最小点覆盖

2017-11-01 16:44:16