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

Sample Input

2 5

ABABA

ABABA

Sample Output

2

Hint

The entire milking grid can be constructed from repetitions of the pattern 'AB'.


简要题意

给你一个字符矩阵问最小的循环子矩阵的大小

思路

考虑kmp,对每一行和每一列分开处理

一个字符串的最小循环节就是\(len - fail[len]\)

然后对所有行的最小循环节取lcm,对所有列的最小循环节取lcm

然后把这两个值乘起来就可以了


//Author: dream_maker
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
//fast read and write
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 10010;
char s1[N][80], s2[80][N];
int fail1[N][80], fail2[N][80];
int n, m;
int gcd(int a, int b) {
return b ? gcd(b, a%b) : a;
}
int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
void getfail(char *s, int *fail, int len) {
fail[1] = 0;
int j = 0;
fu(i, 2, len) {
while (j && s[j + 1] != s[i]) j = fail[j];
if (s[i] == s[j + 1]) ++j;
fail[i] = j;
}
}
int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
Read(n), Read(m);
fu(i, 1, n) {
fu(j, 1, m)
s1[i][j] = s2[j][i] = getchar();
getchar();
}
int tmpn = 1;
fu(i, 1, n) {
getfail(s1[i], fail1[i], m);
tmpn = lcm(tmpn, m - fail1[i][m]);
if (tmpn >= m) {
tmpn = m;
break;
}
}
int tmpm = 1;
fu(i, 1, m) {
getfail(s2[i], fail2[i], n);
tmpm = lcm(tmpm, n - fail2[i][n]);
if (tmpm >= n) {
tmpm = n;
break;
}
}
Write(tmpn * tmpm);
return 0;
}

POJ2185 Milking Grid 【lcm】【KMP】的更多相关文章

  1. poj2185 Milking Grid【KMP】

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10084   Accepted: 4371 Des ...

  2. 【kmp算法】poj2185 Milking Grid

    先对每行求出所有可能的循环节长度(不需要整除). 然后取在所有行中都出现了的,且最小的长度为宽. 然后将每一行看作字符,对所有行求next数组,将n-next[n](对这些行来说最小的循环节长度)作为 ...

  3. [USACO2003][poj2185]Milking Grid(kmp的next的应用)

    题目:http://poj.org/problem?id=2185 题意:就是要求一个字符矩阵的最小覆盖矩阵,可以在末尾不完全重合(即在末尾只要求最小覆盖矩阵的前缀覆盖剩余的尾部就行了) 分析: 先看 ...

  4. POJ2185 Milking Grid KMP两次(二维KMP)较难

    http://poj.org/problem?id=2185   大概算是我学KMP简单题以来最废脑子的KMP题目了 , 当然细节并不是那么多 , 还是码起来很舒服的 , 题目中描写的平铺是那种瓷砖一 ...

  5. POJ2185 Milking Grid 题解 KMP算法

    题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...

  6. poj2185 Milking Grid

    题目链接:http://poj.org/problem?id=2185 这道题我看了好久,最后是通过参考kuangbin的博客才写出来的 感觉next数组的应用自己还是掌握的不够深入 这道题其实就是先 ...

  7. 【POJ2185】【KMP + HASH】Milking Grid

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

  8. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

  9. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

随机推荐

  1. shell 条件语句

    shell 条件语句 #!/bin/bash # 条件语句 NUM1=100 NUM2=200 if (($NUM1 > $NUM2));then echo "$NUM1 greate ...

  2. TensorFlow备忘录——conv2d函数

    卷积函数 TensorFlow学习备忘录 tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_forma ...

  3. 聚类算法——MCL

    最近在看聚类方面的论文,接触到了MCL聚类,在网上找了许久,没什么中文的资料,可能写的最具体的便是GatsbyNewton写的 马尔可夫聚类算法(MCL) 这篇博客了.但是,其中仍有一些不详细的地方. ...

  4. [PostgreSql]PostgreSql调用函数及用IF EXISTS判断表是否存在

    1.创建一个函数function1 -- FUNCTION: public.function1(character varying, integer) -- DROP FUNCTION public. ...

  5. 毕业设计总结(1)-canvas画图

    去年6月底完成的毕业设计,到现在也才开始给它做个总结,里面有很多可以学习和借鉴的东西. 我的毕业设计的题目是“一种路径规划算法的改进与设计”,具体的要求可参见下面的表格: 题目 一种路径规划算法的改进 ...

  6. Python+Opencv进行识别相似图片

    http://blog.csdn.net/feimengjuan/article/details/51279629

  7. Android数据库框架-----ORMLite关联表的使用

    上一篇已经对ORMLite框架做了简单的介绍:Android数据库框架-----ORMLite 的基本用法~~本篇将介绍项目可能会使用到的一些用法,也为我们的使用ORMLite框架总结出一个较合理的用 ...

  8. 明明白白AOP

    引子: AOP(面向方面编程:Aspect Oriented Programing)和IoC一样是Spring容器的内核,声明式事务的功能在此基础上开花结果.但是AOP和OOP差别较大,要很好地理解这 ...

  9. 杀死dialog

    先 pkill -9 normal.sh 和  pkill -9 terminal_ui.sh 然后在pkill -9 dialog

  10. Java开发微信公众号模板消息【同步|异步】

    第一步:申请模板消息功能并添加模板 在微信公众平台找到你需要的模板,并添加上即可: 第二步:添加功能模块后开始开发 功能中使用的类及代码: 发送数据主实体类: Template.java packag ...