POJ 2185 Milking Grid (two-dimensional KMP next array)

Source: Internet
Author: User

Milking Grid
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 6665 Accepted: 2824

Description

Every morning when they was milked, the Farmer John ' s cows form a rectangular grid that's R (1 <= R <=) rows by C (1 <= C <=) columns. As we all know, Farmer John was quite the expert on cow behavior, and was currently writing a book about feeding behavior in Cows. He notices that if each cow was labeled with a uppercase letter indicating its breed, the two-dimensional pattern formed B Y his cows during milking sometimes seems to is made from smaller repeating rectangular patterns.

Help FJ find the rectangular unit of smallest area the can is repetitively tiled to make up the entire milking grid. Note that the dimensions of the small rectangular unit does not necessarily need to divide evenly the dimensions of the Enti Re 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, the cows form, with a uppercase letter denoting each cow ' s breed. Each of the R input lines have 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 5ABABAABABA

Sample Output

2

Hint

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

Source

Usaco 2003 Fall

Title Link: http://poj.org/problem?id=2185

The main topic: give you a r row C column character matrix, so that a sub-matrix, so that this sub-matrix infinitely copied into the large matrix containing the original matrix, and now find the minimum size of the sub-matrix

Topic Analysis: 1. Treat each line of string as a whole row for next array
2. Transpose The Matrix
3. Take action 1, note that the row here is the original column, the column is the original row, equivalent to the original column of the next array
4. Find out Len-next[len] that is, the length of the smallest non-repeating substring as the edge length of the sub-rectangle

#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++) SCA        NF ("%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])); }}



POJ 2185 Milking Grid (two-dimensional KMP next array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.