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 ...
随机推荐
- html5 canvas 实现简单的画图
今天早上看了一下 canvas 前端画图,数据可视化, 百度的 echart.js , d3等 js 库都已经提供了强大的绘制各种图形的 API. 下面记录一下 有关canvas 绘图的基本知识: ...
- ZjDroid工具介绍及脱壳详细示例
前提条件: 1.Root手机一部 2.需要通过Xposed installer(http://dl.xposed.info/latest.apk)安装Xposed Framework; 一.ZjDro ...
- hbase 第一篇
参考:http://www.jdon.com/38244 http://chuanwang66.iteye.com/blog/1683533
- uvc摄像头代码解析1
一.FAQ 1.判断自己的摄像头是否支持uvc标准 输入lsusb //列出usb设备 [cpp] Bus 001 Device 001: ID 1d6b:0002 Linux Foundatio ...
- 控制流(swift)
检测API是否可用 if #available(iOS 9, OSX 10.10, *) { // 在 iOS 使用 iOS 9 APIs , 并且在 OS X 使用 OS X v10.10 APIs ...
- iOS开发——离线缓存
先搭好架子,有时间了再填充.
- spring DateUtils
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreem ...
- foreach笔记
结合泛型使用,不然就只能写成for(Object o : T). 缺点是没有下标,如下面代码 public class ForeachTest { public static void main(St ...
- CastleWindsor 使用说明
1.引用DLL Castle.Core.dll 和Castle.Windsor.dll 2. 引用命名空间 using Castle.MicroKernel.Resolvers.Specialize ...
- C语言-switch语句
switch (表达式的值) { case 1: 语句1 break; case 2: 语句2 break; case 3: 语句3 break; case 4: 语句4 break; ...... ...