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) 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

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

题目大意:给你一个r行c列的字符矩阵,令其一个子矩阵,使得这个子矩阵无限复制成的大矩阵包含原矩阵,现求这个子矩阵的最小尺寸

题目分析:1.把每行字符串看作一个整体对行求next数组

2.将矩阵转置

3.进行操作1,注意这里的行是原来的列,列是原来的行,相当于求原来列的next数组

4.求出len-next[len]即最小不重复子串的长度作为子矩形的边长

#include <cstdio>
#include <cstring>
char s[10005][80], rs[80][10005];
int R[10005], C[10005];
int r, c;

void get_nextR()
{
    R[0] = -1;
    int j = -1, i = 0;
    while(i < r)
    {
        if(j == -1 || strcmp(s[i], s[j]) == 0)
        {
            i++;
            j++;
            R[i] = j;
        }
        else
            j = R[j];
    }
}  

void get_nextC()
{
    C[0] = -1;
    int j = -1, i = 0;
    while(i < c)
    {
        if(j == -1 || strcmp(rs[i], rs[j]) == 0)
        {
            i++;
            j++;
            C[i] = j;
        }
        else
            j = C[j];
    }
} 

int main()
{
    while(scanf("%d %d", &r, &c) != EOF)
    {
        for(int i = 0; i < r; i++)
            scanf("%s", s[i]);
        get_nextR();
        for(int i = 0; i < r; i++)
            for(int j = 0; j < c; j++)
                rs[j][i] = s[i][j];
        get_nextC();
        printf("%d\n", (r - R[r]) * (c - C[c]));
    }
}
时间: 2024-10-27 04:12:21

POJ 2185 Milking Grid (二维KMP next数组)的相关文章

Match:Milking Grid(二维kmp算法)(POJ 2185)

奶牛矩阵 题目大意:给定一个矩阵,要你找到一个最小的矩阵,这个矩阵的无限扩充的矩阵包含着原来的矩阵 思路:乍一看这一题确实很那做,因为我们不知道最小矩阵的位置,但是仔细一想,如果我们能把矩阵都放在左上角该多好,这样一来这一题好像又是循环数组那个样子了(二维的). 而事实上我们确实可以把所有情况都放在左上角,因为矩阵里面的元素的相对位置是不变的,这样一来我们就可以把矩阵看成都是一些元素从左上角往右下角扩充.那么现在问题就又回到了循环节的问题上了,我们可以把矩阵看成是很多很多个字符串组成,我们要找的

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

POJ 2155 Matrix 【二维树状数组】

题目链接:http://poj.org/problem?id=2155 题目大意:给出一个N*N的0矩阵,下面给出两种指令:1. 给出的第一个数据为'C',再给出四个整形数据,x1,y1,y1,y2,对以(x1,y1)(x2,y2)分别为左上角和右下角坐标的矩阵内的元素进行反转(0变1,1变0)         2. 给出的第一个数据为'Q',再给出两个数据,x,y,然后输出此时这个坐标上的元素. 这题用二维树状数组解,树状数组能够对某区间更新所有元素的和,树状数组维护的是c[1][1]到c[i

POJ 2185 Milking Grid

Milking Grid Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 218564-bit integer IO format: %lld      Java class name: Main Every morning when they are milked, the Farmer John's cows form a rectangular grid th

POJ 2155 Matrix【二维树状数组+YY(区间更新,单点查询)】

题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 32950   Accepted: 11943 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th col

POJ 2185 Milking Grid(KMP)

解题思路: 算是一个多维的KMP,其实是一样的,不过把1个字符的比较改成一行或一列字符的比较,对行和列使用两次KMP,最后乘起来就可以了. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include &l

POJ 2185 Milking Grid (搬运KMP)

题意:给你一个字母矩阵,让你找一个最小的字母矩阵,复制后可以得到大的矩阵,问最小矩阵面积是多少,假设原来的矩阵长度是6,你用一个长度为5的串复制,多余的部分不算,只算6以内相同即可 思路:其实没有什么思路,看了一些大佬们的博客,其中有几篇写的很棒 传送门1,传送门2 大体的思路就是找到每行的循环节,然后插入在桶中,在另一侧记录的时候发现等于列的数量是就代表这是最小的(细节部分窝还要想几天) 代码: 不存在的(最近搬运代码越来越熟练了) 原文地址:https://www.cnblogs.com/l

POJ 题目2155 Matrix(二维树状数组)

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20303   Accepted: 7580 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1