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 ...
随机推荐
- ural1890 Money out of Thin Air
Money out of Thin Air Time limit: 1.0 secondMemory limit: 64 MB Each employee of the company Oceanic ...
- PAT (Advanced Level) 1024. Palindromic Number (25)
手动模拟加法高精度. 注意:如果输入数字的就是回文,这个时候输出0步. #include<iostream> #include<cstring> #include<cma ...
- Jobject 使用
obj2 = JObject.Parse(result.Html); JArray _Jarr = _obj["data"]["siteList"].Value ...
- matlab字符串操作
字符串转换函数 abs 字符串到ASCII转换dec2hex 十进制数到十六进制字符串转换fprintf 把格式化的文本写到文件中或显示屏上hex2dec ...
- spring boot + neo4j restful
整整折腾了三天,终于把spring boot + neo4j的路走通了. 这里介绍3个部分,pom,entity,repository 1)pom <?xml version="1.0 ...
- CentOS 7.0 安装配置 kafka 消息队列
查询下载最新版本 kafka http://kafka.apache.org/downloads.html wget http://mirror.bit.edu.cn/apache/kafka/0.8 ...
- 设计模式笔记之一:MVP架构模式入门(转)
写在前面:昨天晚上,公司请来专家讲解了下MVP,并要求今后各自负责的模块都要慢慢的转到MVP模式上来.以前由于能力有限,没有认真关注过设计模式.框架什么的,昨晚突然兴趣大发,故这两天空闲时间一直在学习 ...
- 【转】int && 非常量右值
C++ 11中引入的右值引用正好可用于标识一个非常量右值.C++ 11中用&表示左值引用,用&&表示右值引用,如: int &&a = 10 右值引用根据其修饰 ...
- leetcode day7
这道题弄的心好累.. [Reverse Linked List]206 描述: Reverse a singly linked list. click to show more hints. Hin ...
- 统计学常用概念:T检验、F检验、卡方检验、P值、自由度
1,T检验和F检验的由来 一般而言,为了确定从样本(sample)统计结果推论至总体时所犯错的概率,我们会利用统计学家所开发的一些统计方法,进行统计检定. 通过把所得到的统计检定值,与统计学家建立了一 ...