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 ...
随机推荐
- 2019牛客多校四 E. triples II (容斥)
大意: 给定$n,a$, 求$n$个$3$的倍数, $or$和为$a$的方案数. 简单容斥题 可以求出$f_{x,y}$表示所有$3$的倍数中, 奇数位不超过$x$个$1$, 偶数位不超过$y$个$1 ...
- Geometers Anonymous Club CodeForces - 1195F (闵可夫斯基和)
大意: 给定$n$个凸多边形, $q$个询问, 求$[l,r]$内闵可夫斯基区间和的顶点数. 要用到一个结论, 闵可夫斯基和凸包上的点等于向量种类数. #include <iostream> ...
- ASP.NET 使用 SyndicationFeed 输出 Rss
以前生成 RSS 都是使用拼接 Xml 的方式生成的,不仅麻烦而且还不规范. #region 输出指定分类编号的消息源内容... /// <summary> /// 输出指定分类编号的消息 ...
- 十、es6之扩展运算符 三个点(...)
对象的扩展运算符 对象中的扩展运算符(...)用于取出参数对象中的所有可遍历属性,拷贝到当前对象之中 let bar = { a: 1, b: 2 }; let baz = { ...bar }; / ...
- Array + two points leetcode.15-3Sum
题面 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Fi ...
- Java基础加强-泛型
/*泛型*/ (泛型是给编译器看的) 泛型是提供给 /*javac编译器使用的*/,可以限定集合中的输入类型,让编译器挡住源程序中的非法输入,编译器编译带类型带类型说明的集合时,会去掉 "类 ...
- 开发六年mybatisplus使用小结
最近在项目里用到了一个第三方库,叫mybatisplus,是一个mybatis的增强库,简单来说就是增强了mybatis的功能,让mybatis更好用,mybatisplus给的官方定义是Mybati ...
- Docker的学习1
Docker是开源的应用容器引擎.可以理解为轻量级的虚拟机,又可以理解为开了挂的chroot. 官方解释为docker是一个开源的项目,可以用来将任何应用以轻量级容器的形式,打包,发布和运行. doc ...
- 如何使用Metasploit进行汽车安全性测试
前言 针对汽车的攻击和入侵是当前最前沿的领域和最热门的话题之一.随着自动驾驶汽车技术的发展,在未来这个领域将变得更加重要.作为汽车黑客快速发展的一部分,我最喜欢的黑客工具之一Metasploit也开发 ...
- python3和python2共存
在window上同时安装py3.5和py2.7,但是命令行敲击python命令后,默认只出现py2.7的信息,敲击python3命令,提示未知的命令. 从网上查了一下,虽然环境变量都添加对了,但是可执 ...