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;
}


• 本文已收录于以下专栏：

poj 2185 Milking Grid (最小覆盖矩阵)

• u012936765
• 2015年03月20日 16:41
• 404

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

• u013015642
• 2015年08月06日 19:14
• 1166

poj2185

/*kmp的一道好题，ZOJ1905题的一道升华。 这道题目的核心就在于，先行匹配，取最小公倍数，再列匹配，取最小公倍数，两个一乘就是area。 这题有句话很重要，Note that the di...
• nash142857
• 2012年08月23日 17:29
• 1365

Java实现-最小子串覆盖

• qq_14927217
• 2017年07月05日 10:56
• 644

Minimum Window Substring 最小覆盖子串算法

• fly_yr
• 2016年04月12日 16:13
• 3218

POJ2185 最小覆盖矩阵

• alongela
• 2012年11月18日 20:09
• 2626

Lintcode 最小子串覆盖

• Hk_john
• 2017年03月16日 21:21
• 561

Minimum Window Substring（最小覆盖子串）【面试算法题】

leetcode：Minimum Window Substring（最小覆盖子串）【面试算法题】 分类： leetcode面试算法题2013-09-27 02:29 305人阅读 评论(...
• pi9nc
• 2013年09月27日 22:31
• 1091

POJ 2185 Milking Grid(最小覆盖子矩阵面积KMP)

Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4901   A...
• opm777
• 2013年08月05日 17:14
• 937

poj 2185 Milking Grid（最小矩阵覆盖，二维 kmp 匹配)

• u010794465
• 2013年10月09日 13:00
• 564

举报原因： 您举报文章：POJ 2185 (字符串最小覆盖) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)