题目链接: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. ubuntu16.04安装配置mysql数据库,分割视频为帧图像

    参考http://wiki.ubuntu.org.cn/MySQL%E5%AE%89%E8%A3%85%E6%8C%87%E5%8D%97 版本为5.7 一.安装 安装命令sudo apt-get i ...

  2. bzoj4032: [HEOI2015]最短不公共子串(SAM+DP)

    4032: [HEOI2015]最短不公共子串 题目:传送门 题解: 陈年老题良心%你赛膜爆嘎爷 当初做题...一眼SAM...结果只会两种直接DP的情况... 情况1: 直接设f[i][j] 表示的 ...

  3. MyEclipse 安装svn 插件步骤详情

    方法一:在线安装 打开HELP- > MyEclipse Configuration Center.切换到SoftWare标签页. 点击Add Site 打开对话框,在对话框Name输入Svn, ...

  4. C#学习小记

    1.C#是由微软推出的,基于.Net Framework的面向对象的高级编程语言. 2.C#代码编辑器为Visual Studio,简称VS. 3.Hello World VS中新建Windows控制 ...

  5. 调试相关blogs收集

    Debug Diag官方blog  https://blogs.msdn.microsoft.com/debugdiag/ Tess  https://blogs.msdn.microsoft.com ...

  6. Oracle 新手语法记录

    一.用户 1. 创建用户 语法:create user 用户名 identified by 口令; create user test identified by test; 2. 修改用户 语法:al ...

  7. 想写一个 Sketch 插件 结果 一查不可收拾 ~~ 涉及到 Symbol 符号/ Layer 图层 / Overrides 可替换变量 等等

    var sketch = context.api() var document = sketch.selectedDocument; var selection = document.selected ...

  8. css+html应用实例1:滑动门技术的简单实现

    关于滑动门,现在的页面中好多地方都会用到滑动门,一般用作于导航背景,它的官方解释如下: 滑动门:根据文本自适应大小,根据背景的层叠性制作,并允许他们在彼此之上进行滑动,以创造出一些特殊的效果. 为什么 ...

  9. Spinner与适配器模式总结

    今天开始编辑我的第一篇博客. ------------------------------------------------------------------------------------- ...

  10. Pyhton学习——Day59

    参考博客: http://www.cnblogs.com/wupeiqi/articles/6144178.html Form 1. 验证 2. 生成HTML(保留上次输入内容) 3. 初始化默认是 ...