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

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字符矩阵中找出一个最小子矩阵,使其多次复制所得的矩阵包含原矩阵。N<=10000,M<=75


只需要一行做一个字母求一次,再一列做一个字母求一次就好了,然后和子串是一样的n-fail[n]

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=1e4+,M=;
int n,m,k,fail[N];
char s[N][M];
bool cmp1(int a,int b){
for(int i=;i<=m;i++) if(s[a][i]!=s[b][i]) return false;
return true;
}
bool cmp2(int a,int b){
for(int i=;i<=k;i++) if(s[i][a]!=s[i][b]) return false;
return true;
}
void getFail(int n,bool (*cmp)(int a,int b)){
fail[]=;
for(int i=;i<=n;i++){
int j=fail[i-];
while(j&&!cmp(j+,i)) j=fail[j];
fail[i]=cmp(j+,i)?j+:;
}
} int main(){
// freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%s",s[i]+);
getFail(n,cmp1);
k=n-fail[n];
memset(fail,,sizeof(fail));
getFail(m,cmp2);
printf("%d",k*(m-fail[m]));
}
 

POJ 2185 Milking Grid [KMP]的更多相关文章

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

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

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

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

  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 next数组]

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

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

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

  7. poj 2185 Milking Grid

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

  8. Poj 2165 Milking Grid(kmp)

    Milking Grid Time Limit: 3000MS Memory Limit: 65536K Description Every morning when they are milked, ...

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

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

随机推荐

  1. 构建LVS-DR+Keepalive高可用集群

    ------client----------主LVS----------从LVS------------WEB1-------------WEB2--------- 2.2.2.250  2.2.2. ...

  2. 【android studio】 gradle配置成本地离线zip包

    http://blog.csdn.net/jingleye/article/details/52689260       关键词:distributionUrl修改成本地路径,android stud ...

  3. Python数据类型一:数字与运算符

    数字 一.数值类型 python中支持的数值类型有以下几种: 1.整型(Int) - 通常被称为是整型或整数,是正或负整数,不带小数点.Python3 整型是没有限制大小的,可以当作 Long 类型使 ...

  4. 阻止安卓实体返回键后退的网页js实现

    提供两种解决方法吧,都是网上来的,侵权删,毕竟我等只是搞后端的-- 第一种方法: // 阻止安卓实体键后退 // 页面载入时使用pushState插入一条历史记录 history.pushState( ...

  5. KEIL MDK 查看代码量、RAM使用情况--RO-data、RW-data、ZI-data

    KEIL RVMDK编译后的信息 Code是代码占用的空间, RO-data是 Read Only 只读常量的大小,如const型, RW-data是(Read Write) 初始化了的可读写变量的大 ...

  6. h5学习笔记:vuethink 配置

    vuethink 是一款基于PHP TP5和Vuejs 结合的后台框架,设计起来是使用较为前沿.在使用的过程,需要对这款开源的后台做一些调整和面对一些细节的坑.前段时间也因为有项目需求,所以下载了玩了 ...

  7. VIM命令模式与输入模式切换

     vi编辑器 vi是UNIX和类UNIX环境下的可用于创建文件的屏幕编辑器.vi有两种工作模式:命令模式和文本输入模式.启动vi需要输入vi,按[Spacebar]键并输入文件名后回车. 切换模式键 ...

  8. CGI,FAST-CGI,PHP-FPM的区别

    http://blog.csdn.net/xsgnzb/article/details/52875331 CGI全称Common Gateway Interface即公共网关接口,它遵循cgi规范,定 ...

  9. robots书写说明:

    "User-Agent"搜索引擎的名字. 针对所有搜索引擎写法:User-Agent: *,针对百度搜索引擎写法:User-Agent:Baiduspider "Allo ...

  10. set&enum小结(database)

    今天发现mysql中有set这种数据类型,工作的业务中也使用到了.网上查阅资料后,小结一下 先总结一下两者的分别 set和enum类似表单中的多选和单选,set和enum在数据库内部是用整数表示的,显 ...