POJ 2185 Milking Grid(KMP)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 4738 | Accepted: 1978 |
Description
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
* 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
Sample Input
2 5
ABABA
ABABA
Sample Output
2
Hint
Source
//============================================================================
// Name : POJ.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
using namespace std;
char str[][];
int R,C;
bool same1(int i,int j)//第i行和第j行相等
{
for(int k=;k<C;k++)
if(str[i][k]!=str[j][k])
return false;
return true;
}
bool same2(int i,int j)//第i列和第j列相等
{
for(int k=;k<R;k++)
if(str[k][i]!=str[k][j])
return false;
return true;
}
const int MAXN=;
int next[MAXN];
int main()
{
while(scanf("%d%d",&R,&C)==)
{
for(int i=;i<R;i++)scanf("%s",str[i]);
int i,j;
j=next[]=-;
i=;
while(i<R)
{
while(-!=j && !same1(i,j))j=next[j];
next[++i]=++j;
}
int ans1=R-next[R];
j=next[]=-;
i=;
while(i<C)
{
while(-!=j && !same2(i,j))j=next[j];
next[++i]=++j;
}
int ans2=C-next[C];
printf("%d\n",ans1*ans2);
}
return ;
}
POJ 2185 Milking Grid(KMP)的更多相关文章
- POJ 2185 Milking Grid (KMP,求最小覆盖子矩阵,好题)
题意:给出一个大矩阵,求最小覆盖矩阵,大矩阵可由这个小矩阵拼成.(就如同拼磁砖,允许最后有残缺) 正确解法的参考链接:http://poj.org/showmessage?message_id=153 ...
- POJ 2185 Milking Grid(KMP最小循环节)
http://poj.org/problem?id=2185 题意: 给出一个r行c列的字符矩阵,求最小的覆盖矩阵可以将原矩阵覆盖,覆盖矩阵不必全用完. 思路: 我对于字符串的最小循环节是这么理解的: ...
- 题解报告: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 (二维KMP)
题意:给出一个字符矩形,问找到一个最小的字符矩形,令它无限复制之后包含原来的矩形. 此题用KMP+枚举来做. 一维的字符串匹配问题可以用KMP来解决.但是二维的就很难下手.我们可以将二维问题转化为一维 ...
- poj 2185 Milking Grid(next数组求最小循环节)
题意:求最小的循环矩形 思路:分别求出行.列的最小循环节,乘积即可. #include<iostream> #include<stdio.h> #include<stri ...
- POJ 2185 Milking Grid KMP循环节周期
题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...
- [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(矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Lim ...
- POJ:2185-Milking Grid(KMP找矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Description Every morning when they are milked, ...
随机推荐
- Android权限安全(5)组件的android:exported属性
Android四大组件都有 android:exported 属性 android:exported="true" 时 表示该组件是公开的,其它组件可以访问这个组件 android ...
- Hibernate4.2.2使用Annotation配置
1.在hibernate官网下载hibernate-release-4.2.2.Final.zip并解压 2.新建一个java project工程(20130619_Hibernate4.2.2_An ...
- android gallery 自定义边框+幻灯片
最近在项目中用到图片轮播,试了Gallery,ViewFlipper,ViewPager,感觉Gallery最符合需求,但是Gallery的系统边框很难看,项目中要求用自己的背景图片. 下面来看一下使 ...
- php通过curl调用jpush接口实现消息的推送
public function actionNotifyto() { //$regid = $_REQUEST['regid']; $url = 'https://api.jpush.cn/v3/pu ...
- Android service binder aidl 关系
/********************************************************************************** * Android servic ...
- javascript OOP编辑思想的一个实践参考
<html> <style type="text/css"> .current { background-color: red; } .dv { backg ...
- crtmpserver流媒体服务器的介绍与搭建
crtmpserver流媒体服务器的介绍与搭建 (2012-02-29 11:28) 标签: crtmpserver C++ RTMP Server rtmp Adobe FMS(Flash ...
- 数学语言和程序语言的对比:面向过程与面向集合&命题
共同之处:都使用字符串或数值来引用一个客观实体.当然数字和字符串也可以作为实体对象,这取决于人的解释. 不同之处:数学语句每一行都给出了一个结论, 程序语句的每一行都定义了一个过程.注意这里所指的程序 ...
- JS三级折叠菜单特效 自动收缩其它级
真的很不错!很实用,在IE6.IE7.IE8.FF.chrome等浏览器都正常运行,去掉CSS中 #menu ul中 {height:100px; overflow:auto;} 即可高度自适应 &l ...
- Linux应用层直接操作GPIO
Linux应用层直接操作GPIO 在一个老手的指导下,应用层可以直接操作GPIO,具体指设置GPIO的输入输出以及输出电平高或者低.这个大大地提高了灵活性,官方的文档有GPIO Sysfs Inter ...