Poj 2165 Milking Grid(kmp)
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)的更多相关文章
- POJ 2185 Milking Grid KMP循环节周期
题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...
- POJ 2185 Milking Grid KMP(矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Lim ...
- POJ 2185 Milking Grid [KMP]
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8226 Accepted: 3549 Desc ...
- POJ 2185 Milking Grid(KMP)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4738 Accepted: 1978 Desc ...
- [poj 2185] Milking Grid 解题报告(KMP+最小循环节)
题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...
- POJ 2185 Milking Grid [二维KMP next数组]
传送门 直接转田神的了: Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6665 Accept ...
- 题解报告:poj 2185 Milking Grid(二维kmp)
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- poj 2185 Milking Grid
Milking Grid http://poj.org/problem?id=2185 Time Limit: 3000MS Memory Limit: 65536K Descript ...
- POJ 2185 Milking Grid (KMP,求最小覆盖子矩阵,好题)
题意:给出一个大矩阵,求最小覆盖矩阵,大矩阵可由这个小矩阵拼成.(就如同拼磁砖,允许最后有残缺) 正确解法的参考链接:http://poj.org/showmessage?message_id=153 ...
随机推荐
- gmpy安装使用方法
gmpy是一种C编码的Python扩展模块,提供对GMP(或MPIR)多精度算术库的访问.gmpy 1.17是1.x系列的最终版本,没有进一步的更新计划.所有进一步的开发都在2.x系列(也称为gmpy ...
- hdu 6375 度度熊学队列 (链表模拟)
度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣. 初始时有 N 个空的双端队列(编号为 1 到 N ),你要支持度度熊的 Q 次操作. ①1 u w val 在编号为 u 的队列里加入一个 ...
- 如何查看浏览器保存的密码——通过js代码的方式
打开网站,在密码输入框内鼠标右击,选择“审查元素”(或者按F12),浏览器底部弹出网页的代码,并自动定位到密码框的代码段. 1. 第一种方法 选中元素后,直接在 Console 控制台中输入以下命令( ...
- Java 实现简单的 RPC 框架
RPC 简介 RPC,全称为 Remote Procedure Call,即远程过程调用,它是一个计算机通信协议.它允许像调用本地服务一样调用远程服务.它可以有不同的实现方式,而不需要了解底层网络技术 ...
- vue-cli || webpack 打包的时候css里面写的背景图片的路径出错问题
.bg width 100% position fixed left 0 top 0 height 100vh z-index -1 background url('~@/assets/imgs/bg ...
- CSS属性margin、padding的区别
原始状态 不设置margin和padding的状态 margin 设置外边距之后的状态 padding 设置内边距之后的状态 ,注意是撑开,外框高宽由300px变成450px. 说明:本文为原创作品, ...
- wince如何扫描条码并且在浏览器上查询数据
这个挺简单的,winform也适用 public override void OnGetBarcode(string scanStr) { try { Process.Start("iesa ...
- XML文件解析之DOM解析
XML文件是一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便.基本的解析方式包括DOM解析和SAX解析,具体来说包括DOM解析,SAX解析,DOM4J解 ...
- 连接MySQL报错误代码 ERROR 1045时的解决方案
最近在做网站迁移的时候,遇到了一件很尴尬的事情,远程连接数据连不上了,一直报 错误号码1045 Access denied for user 'root'@xx.xxx.xxx.xx( ...
- STM32 ID (转)
STM32唯一ID(Unique Device ID)的读取方法 (转) 每一个STM32微控制器都自带一个96位的唯一ID,也就是Unique Device ID或称为UID,这个唯一ID在任何 ...