POJ 2185 Milking Grid(KMP)

时间:2022-07-12 13:09:53
Milking Grid
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 4738   Accepted: 1978

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

 
 
 
 
 
做两次KMP
 
行和列分别是len-next[len];
 
最后两个结果相乘就可以了
 
 
//============================================================================
// 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)的更多相关文章

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

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

  2. POJ 2185 Milking Grid(KMP最小循环节)

    http://poj.org/problem?id=2185 题意: 给出一个r行c列的字符矩阵,求最小的覆盖矩阵可以将原矩阵覆盖,覆盖矩阵不必全用完. 思路: 我对于字符串的最小循环节是这么理解的: ...

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

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

  4. POJ 2185 - Milking Grid (二维KMP)

    题意:给出一个字符矩形,问找到一个最小的字符矩形,令它无限复制之后包含原来的矩形. 此题用KMP+枚举来做. 一维的字符串匹配问题可以用KMP来解决.但是二维的就很难下手.我们可以将二维问题转化为一维 ...

  5. poj 2185 Milking Grid(next数组求最小循环节)

    题意:求最小的循环矩形 思路:分别求出行.列的最小循环节,乘积即可. #include<iostream> #include<stdio.h> #include<stri ...

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

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

  7. &lbrack;poj 2185&rsqb; Milking Grid 解题报告(KMP&plus;最小循环节)

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

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

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

  9. POJ:2185-Milking Grid(KMP找矩阵循环节)

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

随机推荐

  1. 关于有默认值的字段在用EF做插入操作时的思考

    今天在用EF做插入操作的时候发现数据库中一个datetime类型的字段(CreateDate)的值居然全部为null.于是赶紧看表结构发现CreateDate字段居然是允许为空的. 虽然为空,但是设置 ...

  2. nRF52系列来袭,Nordic的低功耗蓝牙方案大有可为

      坐落在北欧的挪威不像他的邻居芬兰那样,可以先后依靠NOKIA和愤怒的小鸟在世界科技界享有盛名.在一般人看来,挪威除了一个逐渐式微的Opera浏览器以外,并没有更多拿得出手的科技企业.而事实证明这只 ...

  3. URAL 1073 Square Country(DP)

    题目链接 题意 :这个人要投资地,每块地都是正方形并且边长都是整数,他希望他要买的地尽量的少碎块.每买一块地要付的钱是边长的平方,而且会得到一个一份证书,给你一个钱数,让你求出能得到的证书个数. 思路 ...

  4. C&num;二维码生成与解码

    [窗体效果图] [程序源代码] using System; using System.Collections.Generic; using System.ComponentModel; using S ...

  5. 关于百度地图js api的getCurrentPosition定位不准确的解决方法

    很久之前帮大叔解决了一个gps坐标转换为百度地图坐标的问题.今天大叔又给我讲百度地图定位不准.我查了一下api,用了官方给出的这样一组函数. //创建查询对象 var geolocation = ne ...

  6. map &vert; make&lowbar;pair

    #include <map> void func(std::map<int,std::pair<const char*,int>> &T_map) { st ...

  7. 宝塔linux面板&period;txt

    安装命令: yum -y install screen wget && screen -S bt wget -O install.sh http://103.224.251.79:58 ...

  8. 第五周助教工作总结——NWNU李泓毅

    第五周助教总结 注:因第四次实验安排两个标准时间完成,因此本周未提交完整作业. 本周心得: 第四次实验进行过半,八组同学都在实验课上进行了一次中期总结,并形成书面总结在微信群中讨论. 根据各组同学的中 ...

  9. vue el-tree:默认展开第几级节点

    需求描述: Tree 树形结构,默认展开第二级菜单. 查 element 文档: 解决方法: 设置  :default-expanded-keys 的值为 idArr 数组, <el-tree ...

  10. uoj&num;187&period; 【UR &num;13】Ernd

    http://uoj.ac/problem/187 每个点只能从时间,b+a,b-a三维都不大于它的点转移过来,将点按时间分成尽量少的一些段,每段内三维同时非严格单调,每段内的点可能因为连续选一段而产 ...