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. .aspx页面 引用命名空间 (Import 指令,web.config)

    单个页面 要引用其他命名空间,在页面中写: < %@ import namespace="system.text" %> 注:即可,需要引用多个命名空间,不能写多个na ...

  2. mysql 的行转列 PIVOT 的使用

    语句:SELECT DataDate , PropertyText , DataValue FROM RPT_ReportProperty p WITH ( NOLOCK ) JOIN RPT_Rep ...

  3. zpar使用方法之Chinese Word Segmentation

    第一步在这里: http://people.sutd.edu.sg/~yue_zhang/doc/doc/qs.html 你可以找到这句话, 所以在命令行中分别敲入 make zpar make zp ...

  4. php调用mysql存储过程游标

    <?php $dbtype = 'mysql'; $host = 'localhost'; $dbname = 'test'; $dsn = "$dbtype:host=$host;d ...

  5. MySQL Order By Rand()效率分析

    最近研究了一下MYSQL的随机抽取实现方法.举个例子,要从tablename表中随机提取一条记录,大家一般的写法就是:SELECT * FROM tablename ORDER BY RAND() L ...

  6. java中 引用传递、值传递的理解(数组,自定义类,基本数据类型,String类)

    代码部分: public static void main(String[] args) { testInt(); testString(); testArray(); testX(); } publ ...

  7. 【Error】 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    mysql 登录输入密码有时会碰到如题的错误. 错误描述: Error 1045 (28000): Access denied for user 'root'@'localhost' (using p ...

  8. python3.6环境部署文档

    python3.6环境部署文档   内容 Linux部署Python3.6环境 Mac部署Python3.6环境 Window10部署Python3.6环境 Pycharm安装 1. Linux部署P ...

  9. python __new__和__init__

    转载:http://www.cnblogs.com/tuzkee/p/3540293.html 1 2 3 4 5 6 7 8 class A(object):     def __init__(se ...

  10. Tree各种遍历实现

    数据结构.算法及应用 张宪超主编 科学出版社 1. 数据结构的基本概念知识 数据结构的逻辑结构由数据节点和连接两个节点的边组成. 数据节点的数据类型:整型,实数型,布尔型,字符型,指针数据类型 结构的 ...