题目链接: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. VIM7.4 编译安装 开启python

    https://github.com/wklken/k-vim ./configure --prefix=/usr/local/vim74 \--with-features=huge \--enabl ...

  2. BZOJ 3195 DP

    http://www.cnblogs.com/CXCXCXC/p/5093584.html //By SiriusRen #include <cstdio> using namespace ...

  3. week7_notebooke

    回顾:类:具有相同属相和技能的一类事物对象:类的具体表现class A: country = 'China' #静态变量,静态字段 def __init__(self): #动态变量,方法 self. ...

  4. 源码编译Oprofile

    上菜了翠花:首先编译Oprofile需要三个源码:binutils.popt与oprofile Linux版(由于是在64位的linux系统编译-----敬请参考“在64位linux上编译32位程序” ...

  5. CSS3 过渡、动画、多列、用户界面

    CSS3 过渡.动画.多列.用户界面 transition过渡 transition: transition-property transition-duration transition-timin ...

  6. gdal集成kml库的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 最近要读取kml文件,具体就是把kml文件当作一个矢量文件来读取.我发现gdal是支持集成kml库的.不过集成这个km ...

  7. SQL Server 2014 中,新建登录用户,分配权限,并指定该用户的数据

    一.运行环境 系统:Windows 10数据库:SQL Server 2014数据库名: APP     新建的用户名: app 二.操作步骤 1.打开 MS SQL Server Managemen ...

  8. python3 pymysql学习笔记

    练手项目需要用到mysql就顺手把mysql也学了,这个模块没什么好说的,比较简单,实际整个过程我都是在学mysql语句,但还是发现了一些问题. fetchall() 获取结果集中的所有行 这个函数难 ...

  9. 《图解HTTP》摘要

    网络基础TCP/IP 使用Cookie进行状态管理 HTTP首部 确保Web安全的HTTPS 1.网络基础TCP/IP 2.使用Cookie进行状态管理:HTTP是无状态协议. 3.HTTP首部 HT ...

  10. Pyhton学习——Day58

    From表单验证 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...