题目链接: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. Unable to access the IIS metabase

    https://stackoverflow.com/questions/12859891/error-unable-to-access-the-iis-metabase 解决方法1 On Window ...

  2. redis主从复制,读写分离

    主从复制,读写分离 Master/Slave 是什么 master写入 slave读取 能干嘛 读写分离,更加安全,性能提升 怎么玩 一主二仆.薪火相传.反客为主 周明老师,能够把长篇大论总结的很精辟 ...

  3. 超高性能管线式HTTP请求(实践·原理·实现)

    超高性能管线式HTTP请求(实践·原理·实现) 一.总结 一句话总结:实际pipe早就被http1.1所支持,并且大部分nginx服务器也支持并开启了这一功能. pipe之所以能比常规请求方式性能高出 ...

  4. 关于spring和extjs对接的过程简述

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  5. .NET Core 开发:永远的Hello Word

    从.NET Core发布到现在已经很长一段时间了,园子里的各路大神也和它打的火热,本着与时俱进,啥火学啥的原则,我也开始了我的.NET Core学习之旅. 简介 .NET从2002年发行到现在,从呱呱 ...

  6. BZOJ 3230 后缀数组+ST

    思路: 首先我们已经会了后缀数组求本质不同的子串个数 这道题跟那个差不多 首先我们可以知道按字典序排好的每个后缀之前包含多少本质不同的字串 就是sigma(n-sa[i]+1-ht[i]+bi[i-1 ...

  7. POJ 2665 模拟,,

    It is confirmed that these sections do not overlap with each other. 一句话 就变成水题了,,, // by SiriusRen #i ...

  8. element-ui自定义table表头,修改列标题样式

    elementUI table表格一般的样式是这样的: 但是要改变表头是比较麻烦的一个事情,但是往往有些项目是需要的比如改成如下样式: 一般直接改起来挺麻烦,好在官网提供了一个方法:render-he ...

  9. [ RESTful ] [ API ] 有用的資訊

    1. 淺談 REST 軟體架構風格 (Part.I) - 從了解 REST 到設計 RESTful https://blog.toright.com/posts/725/representationa ...

  10. 微信小程序调试 Webview

    document.querySelectorAll("webview")[1].showDevTools(true);