POJ2185(KMP)
Milking Grid
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 7896 | Accepted: 3408 |
Description
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
* 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
Sample Input
2 5
ABABA
ABABA
Sample Output
2
Hint
//2016.8.17
#include<iostream>
#include<cstdio>
#include<algorithm> using namespace std; const int N = ;
const int M = ;
char grid[N][M];
int nex[N]; int gcd(int a, int b)
{
return b==?a:gcd(b, a%b);
} int lcm(int a, int b)
{
return a/gcd(a, b)*b;
} void getNext(int pos, int n, int fg)//构造next[]数组,fg为标记,0为行,1为列
{
nex[] = -;
for(int i = , fail = -; i < n;)
{
if(fg == && (fail == - || grid[pos][i] == grid[pos][fail]))
{
i++, fail++;
nex[i] = fail;
}else if(fg == && (fail == - || grid[i][pos] == grid[fail][pos]))
{
i++, fail++;
nex[i] = fail;
}else fail = nex[fail];
}
} int main()
{
int n, m, clen, rlen;
while(scanf("%d%d", &n, &m)!=EOF)
{
clen = rlen = ;
for(int i = ; i < n; i++)
scanf("%s", grid[i]);
for(int i = ; i < n; i++)//用最小公倍数找到循环块的宽度
{
getNext(i, m, );
rlen = lcm(rlen, m-nex[m]);//m-nex[m]为该行最小循环节的长度
if(rlen>=m){
rlen = m; break;
}
}
for(int i = ; i < m; i++)//用最小公倍数找到循环块的高度
{
getNext(i, n, );
clen = lcm(clen, n-nex[n]);//n-nex[n]为该列最小循环节的长度
if(clen>=n){
clen = n; break;
}
}
printf("%d\n", clen*rlen);
}
return ;
}
POJ2185(KMP)的更多相关文章
- poj2185 kmp求最小覆盖矩阵,好题!
/* 特征值k=m-next[m]就是最小循环节的长度, m%k就是去末尾遗留长度 */ #include<iostream> #include<cstring> #inclu ...
- 【POJ2185】【KMP + HASH】Milking Grid
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- poj2185 Milking Grid【KMP】
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10084 Accepted: 4371 Des ...
- POJ2185 Milking Grid KMP两次(二维KMP)较难
http://poj.org/problem?id=2185 大概算是我学KMP简单题以来最废脑子的KMP题目了 , 当然细节并不是那么多 , 还是码起来很舒服的 , 题目中描写的平铺是那种瓷砖一 ...
- POJ2185 Milking Grid 【lcm】【KMP】
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- 【kmp算法】poj2185 Milking Grid
先对每行求出所有可能的循环节长度(不需要整除). 然后取在所有行中都出现了的,且最小的长度为宽. 然后将每一行看作字符,对所有行求next数组,将n-next[n](对这些行来说最小的循环节长度)作为 ...
- poj2185(kmp算法next数组求最小循环节,思维)
题目链接:https://vjudge.net/problem/POJ-2185 题意:给定由大写字母组成的r×c矩阵,求最小子矩阵使得该子矩阵能组成这个大矩阵,但并不要求小矩阵刚好组成大矩阵,即边界 ...
- [USACO2003][poj2185]Milking Grid(kmp的next的应用)
题目:http://poj.org/problem?id=2185 题意:就是要求一个字符矩阵的最小覆盖矩阵,可以在末尾不完全重合(即在末尾只要求最小覆盖矩阵的前缀覆盖剩余的尾部就行了) 分析: 先看 ...
- POJ2185 Milking Grid 题解 KMP算法
题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...
随机推荐
- js中判断输入框是否为空(判断是一串空的字符串)
function ltrim(str) { if(str.length==0) return(str); else { var idx=0; while(str.charAt( ...
- CodeForces 614B Gena's Code
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...
- Mac下eclipse导入其他工程中文注释出现乱码解决方案
因为用的是mac版的eclipse,导入其他工程注释出现乱码的情况,找了网上的很多方法,大部分都是说的workspace,在这里修改,但是我修改之后还是乱码,最后发现这样一个方法,才得以解决. 点击 ...
- CentOS tengine mysql 5.7 php 5.6
CentOS 7.x 编译安装 LNMP L 版本是 CentOS 7.x x64版本, N 我们使用tengine 的最新版本,主要原因是因为tengine 默认支持很多的模块. M 这里我们选用 ...
- Jedis使用示例
http://javacrazyer.iteye.com/blog/1840161 http://www.cnblogs.com/edisonfeng/p/3571870.html
- hibernate--ID生成策略--annotation
annotation: @GeneratedValue a) 自定义ID b)auto: 对mysql默认使用auto_increment, 对oracle使用hibernate_sequence c ...
- 子序列和问题 acm
题目描述 给定一个序列 {a1,a2,…,an},定义从a[l]到a[r]的连续子序列的和为sum[l,r],即sum[l,r]=sigma{ai},l<=i<=r.(1<=l< ...
- AOJ2249最短路+最小费用
题意:求出某个点到其他点的最短路,并求出在最短路情况下的最小费用 分析:用dijkstra求出最短路并同时更新最小费用即可,注意的是在最短路长度相同时费用取最小即可 #include <iost ...
- (中等) POJ 1084 Square Destroyer , DLX+可重复覆盖。
Description The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The ...
- 如何在Eclipse中安装PDT插件来开发PHP
之前查过很多PDT的安装方法,60%都是让人直接安装All-in-one的PHP eclipse版本,纯属让人无语,而有些给出的PDT安装链接无法正确下载插件,对此,给出了我安装过的PDT插件下载地址 ...