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

USACO 2003 Fall

/*
kmp.
一开始传参数呵呵了.
忘了n和m相等的情况.
先把行看做一个整体做kmp.
然后把列看做一个整体做kmp.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 10001
#define MAXM 81
using namespace std;
int n,m,k,w,next[MAXN];
char g[MAXN][MAXM];
bool check(int x,int y,bool flag)
{
if(flag)
{
for(int i=1;i<=m;i++) if(g[x][i]!=g[y][i]) return false;
return true;
}
else
{
for(int i=1;i<=k;i++) if(g[i][x]!=g[i][y]) return false;
return true;
}
}
void kmp(int x,bool flag)
{
int p=0;
for(int i=2;i<=x;i++)
{
p=next[i-1];
while(p&&!check(i,p+1,flag)) p=next[p];
if(check(i,p+1,flag)) p++;
next[i]=p;
}
}
void slove()
{
kmp(n,1);k=n-next[n];
memset(next,0,sizeof next);
kmp(m,0);w=m-next[m];
return ;
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>g[i][j];
slove();
printf("%d\n",k*w);
return 0;
}

Poj 2165 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]

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

  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+最小循环节)

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

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

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

  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

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

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

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

随机推荐

  1. 怎样确保页面中的js代码一定是在DOM结构生成之后再调用

    有这样一类问题, 如下所示, 就是在dom结构没有生成时就在js代码中调用了, 此时就会报错: <head> <script> console.log(document.bod ...

  2. (四)CXF之处理Map类型的数据

    一.需求描述 正常来讲webService可以处理Java 数据类型.JavaBean.List等,但是却不能处理Map数据类型.本章讲解如何使用适配器来使得web服务可以处理Map数据类型. 流程: ...

  3. JFinal(1)JFinal helloworld

    ** java的极速开放框架:Final 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展.Restful MVC架构,设 ...

  4. Java Web 深入分析(2) DNS

    DNS :Domain Name System,域名系统 ,通俗的来说需要把我们日常见到的URL 网址信息解析成IP地址,例如 DNS域名解析过程 用户浏览器:用户在浏览器地址栏输入域名进行访问,浏览 ...

  5. SQL 语句使用关键字错误

    异常为SQL Error: 1064, SQLState: 42000 时 ADD ALL ALTER ANALYZE AND AS ASC ASENSITIVE BEFORE BETWEEN BIG ...

  6. 整理一下Promise 的用法

    Promise 的定义 Pormise是JS的异步编程的一种解决方案,在ES6将其写进了语言标准,提供了原生的Promise对象. Promise简单来理解就是一个容器,里面存放着某个未来才会结束的事 ...

  7. H5各种头部meta标签功能大全

    <!DOCTYPE html>  H5标准声明,使用 HTML5 doctype,不区分大小写 <head lang=”en”> 标准的 lang 属性写法 <meta ...

  8. SAP Cloud Platform上Destination属性为odata_gen的具体用途

    今天工作发现,SAP Cloud Platform上创建Destination维护的WebIDEUsage属性很有讲究: 帮助文档:https://help.sap.com/viewer/825270 ...

  9. spring-security2配置精讲(转载)

    本文转载自牛人downpour的帖子: http://www.iteye.com/topic/319965 Spring 论坛上看了不少Spring Security的相关文章.这些文章基本上都还是基 ...

  10. SRX550路由器缓存满了无法在web页面操作解决方法

    SRX550路由器缓存满了无法在web页面操作解决方法   首页出现下图为满的标志,我这个文档就是解决这中情况,让web页面可以操作的 1.  打开命令行,输入用户密码,进入路由器 注意:这里使用te ...