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代码:
 #include<string.h>
#include<cstdio>
const int row_maxn=;
const int col_maxn=;
int row,col,row_prefix[row_maxn],col_prefix[col_maxn];
char mp[row_maxn][col_maxn],tmp[col_maxn][row_maxn];
void get_row_prefix(){
int i=,j=-;
row_prefix[]=-;
while(i<row){
if(j==-||!strcmp(mp[i],mp[j]))row_prefix[++i]=++j;
else j=row_prefix[j];
}
}
void get_col_prefix(){
int i=,j=-;
col_prefix[]=-;
while(i<col){
if(j==-||!strcmp(tmp[i],tmp[j]))col_prefix[++i]=++j;
else j=col_prefix[j];
}
}
int main(){
while(~scanf("%d%d",&row,&col)){
for(int i=;i<row;++i)scanf("%s",mp[i]);
for(int i=;i<row;++i)
for(int j=;j<col;++j)
tmp[j][i]=mp[i][j];
get_row_prefix();
get_col_prefix();
printf("%d\n",(row-row_prefix[row])*(col-col_prefix[col]));
}
return ;
}

题解报告:poj 2185 Milking Grid(二维kmp)的更多相关文章

  1. POJ 2185 Milking Grid [二维KMP next数组]

    传送门 直接转田神的了: Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6665   Accept ...

  2. Match:Milking Grid(二维KMP算法)(POJ 2185)

    奶牛矩阵 题目大意:给定一个矩阵,要你找到一个最小的矩阵,这个矩阵的无限扩充的矩阵包含着原来的矩阵 思路:乍一看这一题确实很那做,因为我们不知道最小矩阵的位置,但是仔细一想,如果我们能把矩阵都放在左上 ...

  3. POJ 2185 Milking Grid KMP循环节周期

    题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...

  4. POJ 2185 - Milking Grid (二维KMP)

    题意:给出一个字符矩形,问找到一个最小的字符矩形,令它无限复制之后包含原来的矩形. 此题用KMP+枚举来做. 一维的字符串匹配问题可以用KMP来解决.但是二维的就很难下手.我们可以将二维问题转化为一维 ...

  5. [poj 2185] Milking Grid 解题报告(KMP+最小循环节)

    题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...

  6. poj 2185 Milking Grid

    Milking Grid http://poj.org/problem?id=2185 Time Limit: 3000MS   Memory Limit: 65536K       Descript ...

  7. 【KMP】POJ 2185 Milking Grid -- Next函数的应用

    题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...

  8. POJ 2185 Milking Grid KMP(矩阵循环节)

                                                            Milking Grid Time Limit: 3000MS   Memory Lim ...

  9. POJ 2185 Milking Grid(KMP)

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4738   Accepted: 1978 Desc ...

随机推荐

  1. 【scrapy】Item Pipeline

    After an item has been scraped by a spider,it is sent to the Item Pipeline which process it through ...

  2. Office2010,PPT,EXCEL如何插入日历控件

    1 在Office2010中插入其他控件,然后找到日历控件   2 十字架随便在Excel中绘制一下,得到一个日历控件,注意此时还是在设计模式下,在设计模式下日历控件不是正常状态,你还是可以双击这个控 ...

  3. 在XX公司工作第二天,维护已有代码

    根据<C++ More Exception>所述的规则: Rule #1: Never write using-directives in header files. Rule #2: N ...

  4. C语言最小生成树prim算法(USACO3.1)

    /* ID: hk945801 LANG: C++ TASK: agrinet */ #include<iostream> #include<cstdio> using nam ...

  5. Essay

    要养成先连续输入一对匹配的字符——比如"("和")",以及"{"和"}"——再在其中填写内容的习惯.如果先填写内容,很容 ...

  6. 【Mongodb教程 第六课 】MongoDB 插入文档

    insert() 方法 要插入数据到 MongoDB 集合,需要使用 MongoDB 的  insert() 或 save() 方法. 语法 insert() 命令的基本语法如下: >db.CO ...

  7. php验证邮箱

    <?php if(isset($_POST['email'])){ $email = $_POST['email']; if(filter_var($email, FILTER_VALIDATE ...

  8. Linq To Entities中的动态排序

    换了工作有一个月了,一样的工作.一样的代码.一样的体力活仍就…… Linq To Entityes 也是不新玩意了,近半年来也一直与之打交道,但一直也没对其深究过.今天新加的功能要对所有列支持排序,这 ...

  9. C项目实践之通讯录管理案例

    1.功能需求分析 通讯录管理案例主要实现对联系人的信息进行添加.显示.查找.删除.更新和保存功能.主要功能需求描述如下: (1)系统主控平台: 充许用户选择想要进行的操作,包括添加联系人信息,显示.查 ...

  10. python 获取代码宿主机名 ip

    1.获取hostname 相同代码 不同宿主机 日志名 互异 且 可识别宿主机 分布式爬虫 https://docs.python.org/3.6/library/socket.html#socket ...