zoukankan      html  css  js  c++  java
  • poj 2185 Milking Grid(二维kmp)

    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'.
    解题思路:首先有个结论:最小覆盖子串(串尾多一小段时,用前缀来覆盖)长度为n-prefix[n](n-prefix[n]),n为串长。虽说此题是二维的,但是我们可以对每一行和每一列字符串都看作一个字符,将其变成一维,分别求行和列的前缀表,那么最小可覆盖子串的长度为n-prefix[n],所以最终最小可覆盖矩阵的面积为:(row-row_prefix[row])*(col-col_prefix[col])。
    AC代码:
     1 #include<string.h>
     2 #include<cstdio>
     3 const int row_maxn=10005;
     4 const int col_maxn=80;
     5 int row,col,row_prefix[row_maxn],col_prefix[col_maxn];
     6 char mp[row_maxn][col_maxn],tmp[col_maxn][row_maxn];
     7 void get_row_prefix(){
     8     int i=0,j=-1;
     9     row_prefix[0]=-1;
    10     while(i<row){
    11         if(j==-1||!strcmp(mp[i],mp[j]))row_prefix[++i]=++j;
    12         else j=row_prefix[j];
    13     }
    14 }
    15 void get_col_prefix(){
    16     int i=0,j=-1;
    17     col_prefix[0]=-1;
    18     while(i<col){
    19         if(j==-1||!strcmp(tmp[i],tmp[j]))col_prefix[++i]=++j;
    20         else j=col_prefix[j];
    21     }
    22 }
    23 int main(){
    24     while(~scanf("%d%d",&row,&col)){
    25         for(int i=0;i<row;++i)scanf("%s",mp[i]);
    26         for(int i=0;i<row;++i)
    27             for(int j=0;j<col;++j)
    28                 tmp[j][i]=mp[i][j];
    29         get_row_prefix();
    30         get_col_prefix();
    31         printf("%d
    ",(row-row_prefix[row])*(col-col_prefix[col]));
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    ASP.Net 显示
    ASP.Net EF框架
    ASP.Net redis存取方法
    ASP.Net 红包
    winform 代码生成textbox ,checkbox
    c# winform 窗体 对话框绑定的值如何填到主窗体问题
    c# / .net wFramework winform 之运行后的窗体窗口可拖动操作
    托管调试助手 "DisconnectedContext":“针对此 RuntimeCallableWrapper 向 COM 上下文 0xd47808 的转换失败,错误如下: 系统调用失败。
    使用C#语言,如何实现EPLAN二次开发 Api插件及菜单展示
    winform 学习之qq邮箱正则验证及常用正则
  • 原文地址:https://www.cnblogs.com/acgoto/p/9462284.html
Copyright © 2011-2022 走看看