POJ 2185 Milking Grid (二維KMP next數組),pojmilking

來源:互聯網
上載者:User

POJ 2185 Milking Grid (二維KMP next數組),pojmilking

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 5ABABAABABA

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]));    }}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.