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. ZooKeeper的API操作(二)(通俗易懂)

    所需要6个jar包,都是解压zookeeper的tar包后里面的. zookeeper-3.4.10.jar    jline-0.094.jar    log4j-1.2.16.jar netty- ...

  2. Linux安装keepalived

    1.下载安装ipvs安装包,进行解压 http://www.keepalived.org/software/ 2.创建安装路径连接 安装环境: yum -y install openssl-devel ...

  3. led,key通用IO的端口

    1 注意通用IO端口, GPBCON 只能控制一个GPBDAT位(对应的位),而GPBUP可以使能GPBCON.

  4. 常数PK系列汇总

    常数PK系列说明: 在AC的情况下得分=\(\sum_{i=1}^{10}{1000-runtime\_on\_point_i}\) RE会显示UKE UPD:之前的数据太水,导致好多题都在9000分 ...

  5. 第六天 文件的基本管理和xfs文进系统备份恢复

    1.1 Linux系统目录结构,相对路径/绝对路径 1.1.1 Linux系统目录结构 在linux系统中一切都是文件 / 根目录,一切的起点,就像是一个树杈一样,他是所有叉的根 /bin 在单用户模 ...

  6. OpenGL全景视频

    全景视频其实在实现上和一般的视频播放基本差不多,解码可以用ffmpeg,只是对解码后的图片在绘制的时候要绘制在一个球上(我这里是球,好像有说有的格式要绘制在四面体上的,美做深入研究),而不是画在一个表 ...

  7. ansible入门四(Ansible playbook基础组件介绍)

    本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...

  8. vue打包体积优化之旅

    webpack 与 vue 在使用vue开发时,遇到打包后单个文件太大,因而需要分包,不然加载时间太久.虽然尽可能减少请求次数,但是单个包太大也不是好事 思路 组件按需加载 vue-router 的懒 ...

  9. 登录后保存token值到cookie中

    1.引入相应JS <script src="web/js/jquery-1.9.1.min.js"></script> <script src=&qu ...

  10. 快速切题 poj 1002 487-3279 按规则处理 模拟 难度:0

    487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 247781   Accepted: 44015 Descr ...