题目链接:http://poj.org/problem?id=2185

题目:

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 
 

题目大意:

给出一个矩阵,求最小的可以通过复制包含原矩阵的矩阵大小

题解:

对每一行分别求最小循环节,答案矩阵的宽度就是这些最小循环节的lcm

同理列也是这样处理


#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std; const int N=1e4+;
int r,c,n,m;
int next[N];
char buf[N][],tmp[N];
int kmp(int l)
{
next[]=;
for (int i=,j=;i<=l;i++)
{
while (j&&tmp[i]!=tmp[j+]) j=next[j];
if (tmp[i]==tmp[j+]) j++;
next[i]=j;
}
return l-next[l];
}
int gcd(int x,int y)
{
if (!y) return x;
return gcd(y,x%y);
}
int lcm(int x,int y)
{
return x/gcd(x,y)*y;
}
int main()
{
scanf("%d%d",&r,&c);
for (int i=;i<=r;i++)
{
scanf("%s",buf[i]+);
}
int n=,m=;
for (int i=;i<=r;i++)
{
for (int j=;j<=c;j++) tmp[j]=buf[i][j];
n=lcm(n,kmp(c));
if (n>c)
{
n=c;
break;
}
}
for (int i=;i<=c;i++)
{
for (int j=;j<=r;j++) tmp[j]=buf[j][i];
m=lcm(m,kmp(r));
if (m>r)
{
m=r;
break;
}
}
printf("%d\n",n*m);
return ;
}

[poj 2185] Milking Grid 解题报告(KMP+最小循环节)的更多相关文章

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

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

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

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

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

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

  4. HDU 1358 Period(KMP+最小循环节)题解

    思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...

  5. HDU 3746 Cyclic Nacklace(KMP+最小循环节)题解

    思路: 最小循环节的解释在这里,有人证明了那么就很好计算了 之前对KMP了解不是很深啊,就很容易做错,特别是对fail的理解 注意一下这里getFail的不同含义 代码: #include<io ...

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

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

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

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

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

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

  9. POJ 2185 Milking Grid(KMP)

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4738   Accepted: 1978 Desc ...

随机推荐

  1. bzoj3998: [TJOI2015]弦论(SAM+dfs)

    3998: [TJOI2015]弦论 题目:传送门 题解: SAM的入门题目(很好的复习了SAM并加强Right集合的使用) 其实对于第K小的字符串直接从root开始一通DFS就好,因为son边是直接 ...

  2. 升级Xcode8后的相机crash问题-IOS10权限问题

    当我升级到Xcode8后,启动我的相机项目,直接crash,输出的日志如下: '2016-07-08 16:41:11.268943 project-name[362:56625] [MC] Syst ...

  3. svn是什么

    svn是什么 SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Sub ...

  4. 1.Thinkphp入门--框架介绍

    1.Thinkphp款家最早诞生于2006年初,最初名叫fsc,2007年元旦改名thinkphp,由国人自主开发的框架,有中文的文档和社区 2.Tinkphp下载: 官网:http://www.th ...

  5. Centos7 minimal 系列之Nginx搭建(三)

    一.安装nginx 1.1.安装依赖包 yum -y install gcc-c++ yum -y install pcre pcre-devel yum -y install zlib zlib-d ...

  6. Codeforces 994B. Knights of a Polygonal Table

    解题思路 将骑士按力量从小到大排序,到第i个骑士的时候,前面的i-1个骑士他都可以击败,找出金币最多的k个. 用multiset存金币最多的k个骑士的金币数,如果多余k个,则删除金币数最小的,直到只有 ...

  7. web.xml中的url-pattern写法规则及匹配过程

    servlet和filter在javaEE开发中很常用,因此有必要知道web.xml文件映射的规则 1.  写法 ①完全匹配:以“/”开头,以字母(非“*”)结束    如:<url-patte ...

  8. c#制作简单启动画面的方法

    本文实例讲述了c#制作简单启动画面的方法.分享给大家供大家参考.具体分析如下: 启动画面是程序启动加载组件时一个让用户稍微耐心等待的提示框.一个好的软件在有启动等待需求时必定做一个启动画面.启动画面可 ...

  9. java根据出生日期计算年龄

    /** * @author jerry.chen * @param brithday * @return * @throws ParseException * 根据生日获取年龄; */ public ...

  10. (2)RDD的基本操作

    一.map操作,map(Transform) 二.collect操作,collect(Action) 三.使用PairRDD来做计算,类似key-value结构 采用groupByKey来.将资料按照 ...