Milking Grid
Time Limit: 3000MS   Memory Limit: 65536K
     

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

 
题意:

在N*M字符矩阵中找出一个最小子矩阵,使其多次复制所得的矩阵包含原矩阵,输出最小矩阵面积
样例解释:多次复制 AB
解法:KMP
将矩阵每一行看做一个单位,对这n个单位哈希后做KMP,求出最短循环节len1
将矩阵每一列看做一个单位,对这m个单位哈希后做KMP,求出最短循环节len2
答案=len1*len2
#include<cstdio>
#include<iostream>
#define scale 26
using namespace std;
int n,m;
int a[10001][76];
long long horizontal[10001],vertical[77];
int fh[10011],fv[81];
void kmp()
{
for(int i=1;i<n;i++)
{
int j=fh[i];
while(j&&horizontal[j]!=horizontal[i]) j=fh[j];
fh[i+1]= horizontal[i]==horizontal[j] ? j+1 : 0 ;
}
for(int i=1;i<m;i++)
{
int j=fv[i];
while(j&&vertical[i]!=vertical[j]) j=fv[j];
fv[i+1]= vertical[i]==vertical[j] ? j+1 : 0 ;
}
}
int main()
{
scanf("%d%d",&n,&m);
char c;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cin>>c;
a[i][j]=c-'A'+1;
horizontal[i-1]=horizontal[i-1]*scale+a[i][j];
vertical[j-1]=vertical[j-1]*scale+a[i][j];
}
kmp();
printf("%d",(n-fh[n])*(m-fv[m]));
}

  

poj 2185 Milking Grid的更多相关文章

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

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

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

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

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

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

  4. POJ 2185 Milking Grid(KMP)

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

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

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

  6. POJ 2185 Milking Grid [KMP]

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8226   Accepted: 3549 Desc ...

  7. 题解报告:poj 2185 Milking Grid(二维kmp)

    Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...

  8. POJ 2185 Milking Grid (KMP,求最小覆盖子矩阵,好题)

    题意:给出一个大矩阵,求最小覆盖矩阵,大矩阵可由这个小矩阵拼成.(就如同拼磁砖,允许最后有残缺) 正确解法的参考链接:http://poj.org/showmessage?message_id=153 ...

  9. POJ 2185 Milking Grid(KMP最小循环节)

    http://poj.org/problem?id=2185 题意: 给出一个r行c列的字符矩阵,求最小的覆盖矩阵可以将原矩阵覆盖,覆盖矩阵不必全用完. 思路: 我对于字符串的最小循环节是这么理解的: ...

随机推荐

  1. HttpContext.Current.Server.MapPath("/") 未将对象设置到对象的实例异常。

    多线程中的System.Web.HttpContext.Current.Server.MapPath("/") 多线程中Server.MapPath会失效... 网上找到几种解决方 ...

  2. iOS UIView性能最优的设计圆角并且绘制边框颜色

    //以给cell切圆角为例- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollection ...

  3. Beta阶段DAY3

    一.提供当天站立式会议照片一张 二.每个人的工作 1.讨论项目每个成员的昨天进展 刘阳航:尝试改进UI,美化界面. 林庭亦:调整难度设置. 郑子熙:尝试改进UI,美化界面. 陈文俊:调整难度设置. 2 ...

  4. 查看ROS最大并发连接数量

    命令行下输入以下 ip firewall connection tracking print interval 1 max-entries这个就是最大的并发连接数量 退出按Q 

  5. [总结] Visual Studio 报价已经对比

    来源微软官方网站 对比 https://visualstudio.microsoft.com/zh-hans/vs/compare/?rr=https%3A%2F%2Fwww.ithome.com%2 ...

  6. js本地储存userData实例

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  7. Windows下CURL扩展无效之终极解决办法。

    本地开发环境使用WAMP快速搭建,在使用PHP的CURL时可能会存在无法载入情况,这里提供终极解决方法. 1.在php.ini配置文件中启用 php_curll.dll 扩展: (环境已经自动附带 l ...

  8. 【Python】python 2 map() reduce()

    利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字.输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']. ...

  9. SGU326_Perspective

    NBA打比赛.所有的比赛被分为多个团队.有的比赛是团内的,有的是与团外的队伍打的. 给出团内每个队伍已得分,以及总共有多少场比赛,还有团内所有队伍之间有多少场比赛? 问1队是否可能是分数最高的一个队伍 ...

  10. HDU4183_Pahom on Water

    题意为给你若干个圆,每个圆的颜色对应一个频率,如果两个圆有公共部分,那么这两个圆之间可以走,你可以从起点开始,从频率小的圆走向频率大的圆并且到达终点后,从频率大的圆走向频率小的圆,最终回到起点,路径中 ...