题目链接: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. Linux下FFmpeg的安装编译过程【转】

    本文转载自:http://www.linuxidc.com/Linux/2013-06/85628.htm 详细说下在Linux下FFmpeg的安装编译过程.参考 Ubuntu 10.04安装编译FF ...

  2. Linux下将PHP添加到环境变量,将Mysql加入环境变量。

    1.修改/etc/profile vi /etc/profile 2.添加两行 PATH=$PATH:/usr/local/php7/bin:/usr/local/mysql/bin export P ...

  3. python 从bulkblacklist信誉查询网站提交查询

    import urllib import urllib2 #import webbrowser import re import socket def is_domain_in_black_list( ...

  4. 9. Palindrome Number[E]回文数

    题目 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same b ...

  5. Oracle Access和filter的区别

    在查看Oracle执行计划的时候经常会遇到Access和filter,脑容量太小,总是分不清两者的区别...稍作整理. Access:表示对应的谓词条件会影响数据的访问路径(是按照索引还是表) Fil ...

  6. JavaScript中闭包的理解

    1.什么是闭包 我个人理解闭包就是函数中嵌套函数,但是嵌套的那个函数必须是返回值,才构成闭包: <!DOCTYPE html> <html> <head> < ...

  7. Android ImageView 替换图片

    网上找了半天,找到的都是错的,都不是我想要的效果.我想要的是点击一个图片后,图片被替换. 通过一下方法可以实现:“v”是ImageView对象,“image_name”是替换后的图片资源 ((Imag ...

  8. spring使用注解开发

    1.准备工作(1)导入common-annotations.jar(2)导入schema文件 文件名为spring-context-2.5.xsd(3)在xml的beans节点中配置 service层 ...

  9. Unity5.X 开发资源介绍

    Asset 资源 Category 类别 Publisher 开发商 Rating 评级 Version 版本号   Windows → Asset Store 资源商店 [Ctrl + 9]   U ...

  10. K3内部表数据名称

    在后台数据库ICClassType表中,字段FID<0的是老单,FID>0的是新单.----------------系统设置------------------------FStatus: ...