POJ 2185 (字符串最小覆盖)

原创 2012年03月29日 08:35:00
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(最小覆盖子矩阵面积KMP)

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

二维KMP - 求字符矩阵的最小覆盖矩阵 - poj 2185

Milking Grid    Problem's Link:http://poj.org/problem?id=2185   Mean:   给你一个n*m的字符矩阵,让你求这个字符矩阵的...

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

给一个字符矩阵,求最小覆盖矩阵的面积。 有一个结论:最小覆盖子串(串尾多一小段时,用前缀覆盖)长度为n-next[n](n-pre[n]),n为串长。 #include #include ...

POJ2185 Milking Grid 最小覆盖子矩阵(二维KMP)

题目大意:有一个R×C规模的字符矩阵,求出最小的子矩阵,使该矩阵可以由子矩阵平移得到,输出子矩阵的大小。 分析:很明显,符合条件的子矩阵(设该矩阵的规模为w×h)需要同时满足: (1)该子...

POJ2185--KMP最小覆盖串

Description Every morning when they are milked, the Farmer John's cows form a rectangular grid th...
  • a305657
  • a305657
  • 2014年03月27日 15:16
  • 386

poj2185 Milking Grid (最小覆盖矩阵)

//给定一个由字符组成的矩阵,求出它的面积最小的覆盖矩阵 //可以求出每一行的最小覆盖子串的长度,只要对这些长度求最小公倍数,就可以获得最小覆盖矩阵的宽度。 //同理,求出每一列的最小覆盖子串的长度,...
  • lp_opai
  • lp_opai
  • 2014年08月14日 22:13
  • 895

poj1509 字符串最小表示法

Glass Beads Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 2708   Accepted: 1573...

POJ 2185 Milking Grid 二维kmp

题意:给出一个矩阵,求最小的子矩阵,使得原矩阵包含在子矩阵的某个扩展中。 比如 ABABA ABABA 就是被包含在AB扩展的矩阵中。 输出这个最小的子矩阵的大小。 其实只要两遍KMP就行了,把每...
  • hcbbt
  • hcbbt
  • 2013年11月29日 13:08
  • 1809

【poj 2185 Milking Grid】 KMP*2

Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7365 Accepted: 31...
  • ALPS233
  • ALPS233
  • 2016年01月18日 20:54
  • 450

poj 2185 (KMP)

题意:输入字符矩阵,求最小的矩阵
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 2185 (字符串最小覆盖)
举报原因:
原因补充:

(最多只允许输入30个字)