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

Milking Grid
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 4901 Accepted: 2043

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 

Sample Input

2 5
ABABA
ABABA

Sample Output

2

              题目大意:给你一个n*m的矩阵,求最小覆盖子矩阵的面积,例如样例就是AB,可以覆盖4个AB.但是Note that the dimensions of the small rectangular unit do not necessarily need to divide evenly the dimensions of the entire milking grid.后面可以留有空档.但是上面的换成ABABC,其它都不变,答案应该输出几呢?答案是5,只能这样解释,最小覆盖矩阵实际的意义是把整个都覆盖掉,还可以有多余的。比如上面的覆盖的话就需要2*6AB把它覆盖掉,多余的就不管他。
  
        解题思路:经过分析题意,可以知道,直接KMP每一行的最小循环节,求他们的lcm,记为最小覆盖子矩阵的chang。同理得到kuan,然后两个相乘得到面积。需要注意的是lcm不能大于给定农场的长,宽.

        题目地址:Milking Grid

AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
char a[10005][80];
int lenr,lenc,next[10005];

int getnextr(int r)  //某一行的最小循环节
{
     int i,j;
     next[0]=0,next[1]=0;
     for(i=1;i<lenc;i++)
     {
         j=next[i];
         while(j&&a[r][i]!=a[r][j])
            j=next[j];
         if(a[r][i]==a[r][j])
            next[i+1]=j+1;
          else
            next[i+1]=0;
     }
     return lenc-next[lenc];
}

int getnextc(int c)  //某一列的最小循环节
{
     int i,j;
     next[0]=0,next[1]=0;
     for(i=1;i<lenr;i++)
     {
         j=next[i];
         while(j&&a[i][c]!=a[j][c])
            j=next[j];
         if(a[i][c]==a[j][c])
            next[i+1]=j+1;
          else
            next[i+1]=0;
     }
     return lenr-next[lenr];
}

int gcd(int a,int b)  //求最大公约数
{
     int t;
     while(b) t=a%b,a=b,b=t;
     return a;
}
int lcm(int a,int b)   //求最小公倍数
{
     return a/gcd(a,b)*b;
}

int main()
{
     int i,j;
     while(~scanf("%d%d",&lenr,&lenc))
     {
          for(i=0;i<lenr;i++)
            scanf("%s",a[i]);
          int x=getnextr(0);
          int chang=1,kuan=1;
          for(i=0;i<lenr;i++)
          {
               int tmp=getnextr(i);
               chang = lcm(chang,tmp);
          }
          for(i=0;i<lenc;i++)
          {
               int tmp=getnextc(i);
               kuan = lcm(kuan,tmp);
          }
          if(chang>lenc)  chang=lenc;  //不能超过限度
          if(kuan>lenr) kuan=lenr;     //因为有重复的那种情况(AZAZA).
          printf("%d\n",chang*kuan);
     }
     return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值