POJ2185 Milking Grid 【lcm】【KMP】
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.
OutputLine 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】的更多相关文章
- poj2185 Milking Grid【KMP】
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10084 Accepted: 4371 Des ...
- 【kmp算法】poj2185 Milking Grid
先对每行求出所有可能的循环节长度(不需要整除). 然后取在所有行中都出现了的,且最小的长度为宽. 然后将每一行看作字符,对所有行求next数组,将n-next[n](对这些行来说最小的循环节长度)作为 ...
- [USACO2003][poj2185]Milking Grid(kmp的next的应用)
题目:http://poj.org/problem?id=2185 题意:就是要求一个字符矩阵的最小覆盖矩阵,可以在末尾不完全重合(即在末尾只要求最小覆盖矩阵的前缀覆盖剩余的尾部就行了) 分析: 先看 ...
- POJ2185 Milking Grid KMP两次(二维KMP)较难
http://poj.org/problem?id=2185 大概算是我学KMP简单题以来最废脑子的KMP题目了 , 当然细节并不是那么多 , 还是码起来很舒服的 , 题目中描写的平铺是那种瓷砖一 ...
- POJ2185 Milking Grid 题解 KMP算法
题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...
- poj2185 Milking Grid
题目链接:http://poj.org/problem?id=2185 这道题我看了好久,最后是通过参考kuangbin的博客才写出来的 感觉next数组的应用自己还是掌握的不够深入 这道题其实就是先 ...
- 【POJ2185】【KMP + HASH】Milking Grid
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- 【KMP】Censoring
[KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
随机推荐
- centos7 systemctl一些用法
systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体. systemctl is-enabled servicename.service #查询服务是否 ...
- CGI-FASTCGI-PHPFPM
随意记录,摘自知乎 原文链接:https://segmentfault.com/q/1010000000256516 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式 ...
- CountDownLatch详解
功能描述 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 常见用法 多个人等一个信号后继续执行操作.例如5个运动员,等一个发令员的枪响. 一个人等多个人的信号. ...
- python selenium常用基本方法---H5和键盘鼠标操作
一.模拟手机打开页面(H5测试) from selenium import webdriver mobile_emulation = {'deviceName':'iPhone X'} options ...
- Srping整合EhCache
引入的Jar包如下:
- WeChat-扫码支付
官方文档API: 打开连接 主要实现功能,网站上调起 微信支付二维码图片. 所需引用基类API:Data.cs.WxPayApi.cs.HttpService.cs.Config.cs.Thought ...
- Markdown_01_基础语法
目录 概览 一.区块元素{#BlockElement} 1.段落和换行 2.标题 2.区块引用 2.1 在每行的最前面加上 > 2.2 只在整个段落的第一行最前面加上> 2.3 区块引用可 ...
- vue 插件(Sublime Text 3 常用插件以及安装方法)(转)
使用Package Control组件安装 也可以安装package control组件,然后直接在线安装:按Ctrl+` 调出console粘贴以下代码到底部命令行并回车: { import url ...
- 本地如何搭建IPv6环境测试你的APP(转)
IPv6的简介 IPv4 和 IPv6的区别就是 IP 地址前者是 .(dot)分割,后者是以 :(冒号)分割的(更多详细信息自行搜索). PS:在使用 IPv6 的热点时候,记得手机开 飞行模式 哦 ...
- kali linux下不能以root权限运行vlc的解决办法
习惯了在Linux下面使用VLC播放视频和音乐, 但是 VLC 的 linux 版本并不支持在root下面运行. 终端运行vlc命令报错,错误信息如下 root@kbdancer:~# vlc VLC ...