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. 对于Redis的了解

    Redis :高性能的key-value数据库,支持存储的value类型包括字符串.链表.集合.有序集合.哈希类型. redis使用两种文件格式:全量数据和增量请求. 全量数据格式是将内存中的数据写入 ...

  2. 简易版本vue的实现和注解

    本文参考的是前辈的简易版本Vue实现:http://www.cnblogs.com/canfoo/p/6891868.html,感谢.前辈GitHub地址:https://github.com/can ...

  3. 读书笔记-《Linux/Unix设计思想》

    本书主要讲的是Unix程序设计思想,具体涉及到linux的内容不多. 整本书的一个基本出发点是开源.其中主要强调的观点包括: 1.小即是美 作者持有的主要观点是程序应该以小为美.小程序实现小功能,每个 ...

  4. 半夜思考之查漏补缺, Spring 中的容器后处理器

    之前学 Spring 的时候 , 还没听过容器后处理器 , 但是一旦写出来 , 就会觉得似曾相识 . 容器配置器通常用于对 Spring 容器进行处理 , 并且总是在容器实例化任何其他 Bean 之前 ...

  5. BZOJ5120 无限之环(费用流)

    方案合法相当于要求接口之间配对,黑白染色一波,考虑网络流.有一个很奇怪的限制是不能旋转直线型水管,考虑非直线型水管有什么特殊性,可以发现其接口都是连续的.那么对于旋转水管,可以看做是把顺/逆时针方向上 ...

  6. Vector源码解析

    概要 学完ArrayList和LinkedList之后,我们接着学习Vector.学习方式还是和之前一样,先对Vector有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它.第1部分 Vec ...

  7. ibatis解决sql注入问题

    原文: http://blog.csdn.net/scorpio3k/article/details/7610973 对于ibaits参数引用可以使用#和$两种写法,其中#写法会采用预编译方式,将转义 ...

  8. java追加写入txt文件

    整理了下网上的资料,数据追加写入txt文件有三种方式,见下面代码: 方法一: public void method1() { FileWriter fw = null; try { //如果文件存在, ...

  9. ZJOI 2018 一试记

    ZJOI一试几天,天微冷,雨.倒是考试当天近午时分出了太阳. 开题前的一刻,心情反而平静了,窗外泛着淡金色的日光照进来,仿佛今天的我并不是所谓来冲击省队,而只是来经历一场洗礼. 开题了,虽然有一点小插 ...

  10. P3320 [SDOI2015]寻宝游戏 解题报告

    P3320 [SDOI2015]寻宝游戏 题目描述 小B最近正在玩一个寻宝游戏,这个游戏的地图中有\(N\)个村庄和\(N-1\)条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以 ...