poj2185 Milking Grid(KMP运用)

题目链接:http://poj.org/problem?id=2185

Milking Grid

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 6249   Accepted: 2616

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

Hint

The entire milking grid can be constructed from repetitions of the pattern ‘AB‘.

Source

USACO 2003 Fall

思路:

求出每一行的最小重复子串的长度,所有行的最小重复串的长度的LCM就是最小重复子矩阵的宽。然后对列也做相同的操作。于是就可以求得最小重复子矩阵的大小了。(这里要注意一点:当所得的宽大于原来的宽时,就让等于原来的宽,长也是如此)。

代码如下:

#include<cstdio>
#include<cstring>
#define MAXN 10017
int next[MAXN];
int len;
int row, col;
int subrow[MAXN];
int subcol[87];
char s[MAXN][87];

int LCM(int a, int b)//最小公倍数
{
    int x = a, y = b;
    int r = x%y;
    while(r > 0)
    {
        x = y;
        y = r;
        r = x % y;
    }
    return a*b/y;
}

int getnext_r(int r)//行
{
    int i = 0, j = -1;
    next[0] = -1;
    while(i < col)
    {
        if(j == -1 || s[r][i] == s[r][j])
        {
            i++;
            j++;
            next[i] = j;
        }
        else
            j = next[j];
    }
    return col - next[col];
}
int getnext_c(int c)//列
{
    int i = 0, j = -1;
    next[0] = -1;
    while(i < row)
    {
        if(j == -1 || s[i][c] == s[j][c])
        {
            i++;
            j++;
            next[i] = j;
        }
        else
            j = next[j];
    }
    return row - next[row];
}
int main()
{
    while(~scanf("%d%d",&row, &col))
    {
        for(int i = 0; i < row; i++)
        {
            scanf("%s",s[i]);
        }
        for(int i = 0; i < row; i++)//统计每一行的重复子串
        {
            subrow[i] = getnext_r(i);
        }
        for(int i = 0; i < col; i++)//统计每一列的重复子串
        {
            subcol[i] = getnext_c(i);
        }
        int R = 1;
        for(int i = 0; i < row; i++)//统计所有行的重复子串的最小公倍数
        {
            R = LCM(R,subrow[i]);
        }
        if(R > col)//如果最小公倍数大于了列数就取列数
            R = col;
        int C = 1;
        for(int i = 0; i < col; i++)//统计所有列的重复子串的最小公倍数
        {
            C = LCM(C,subcol[i]);
        }
        if(C > row)//如果最小公倍数大于了行数就取行数
            C = row;
        int ans = R * C;
        printf("%d\n",ans);
    }
    return 0;
}

poj2185 Milking Grid(KMP运用)

时间: 2024-11-05 02:33:23

poj2185 Milking Grid(KMP运用)的相关文章

POJ2185 Milking Grid(KMP)

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

poj2185 Milking Grid (最小覆盖矩阵)

//给定一个由字符组成的矩阵,求出它的面积最小的覆盖矩阵 //可以求出每一行的最小覆盖子串的长度,只要对这些长度求最小公倍数,就可以获得最小覆盖矩阵的宽度. //同理,求出每一列的最小覆盖子串的长度,再求最小公倍数,就可以获得最小覆盖矩阵的高度了. # include <stdio.h> # include <string.h> # include <algorithm> using namespace std; char a[10010][100]; int next

poj 2185 Milking Grid(KMP)

题目链接:poj 2185 Milking Grid 题目大意:给定一个N?M的矩阵,找到一个最小的r?c的矩阵,使得原矩阵可以由若干个小矩阵组成,输出r?c的值. 解题思路:将行和列分别看成字符串,然后hash,对hash后的数组分别求KMP. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef unsigned long long ll; con

POJ 2185 Milking Grid KMP循环节周期

题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一个AB 组成一个 ABABAB ABABAB 能够多出来 思路:每一行求出周期 总共n个 求这n个周期的最小公倍数 假设大于m 取m 每一列求出周期 总共m个求这个m个周期的最小公倍数 假设大于n取n 答案就是2个最小公倍数的积 #include <cstdio> #include <cst

【kmp算法】poj2185 Milking Grid

先对每行求出所有可能的循环节长度(不需要整除). 然后取在所有行中都出现了的,且最小的长度为宽. 然后将每一行看作字符,对所有行求next数组,将n-next[n](对这些行来说最小的循环节长度)作为长. 最后输出长乘宽即可. #include<cstdio> #include<cstring> using namespace std; bool can[10010][80]; char s[10010][80]; int next[80],n,m,wide,NEXT[10010]

POJ2185 Milking Grid【KMP】

题目链接: http://poj.org/problem?id=2185 题目大意: 有一个N行M列的字符矩阵,这个字符矩阵可以由较小的矩阵重复平铺组成整个矩阵.问: 最小的字符子矩阵的面积为多少. 思路: 对于长度为M的每一行s[i]来说,M-Next[M],M-Next[Next[M]],-都是能通过复制,完 全覆盖字符串的可行串,而M-Next[M]是最小的.遍历每一行,求出对所有s[i]都可行的最 小字符串长度,即每一行M-Next[M]的最小公倍数lcmn.再用类似的方法求出长度为N.

【POJ2185】【KMP + HASH】Milking Grid

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 wri

Milking Grid poj2185

Milking Grid POJ - 2185 时限: 3000MS   内存: 65536KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 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.

POJ 2185 Milking Grid (二维KMP next数组)

Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6665   Accepted: 2824 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