程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 2185 Milking Grid (二維KMP next數組)

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

編輯:C++入門知識

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



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved