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 ...
随机推荐
- PAT (Advanced Level) 1082. Read Number in Chinese (25)
模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- h2database. 官方文档
http://www.h2database.com/html/advanced.html http://www.h2database.com/html/tutorial.html#csv http:/ ...
- iOS之UIColloctionView
iOS--UICollectionView(滚动视图)入门 UICollectionView @interface UICollectionView : UIScrollView UICollecti ...
- SpringMVC接收页面表单参数-java-电脑编程网
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- S3C2440的SPI解析
位串行数据的频率.如果只希望发送,则接收数据可以保持伪位(dummy).此外如果只希望接收,则需要发送伪位'1'数据 使用SPI主要需要以下寄存器 选择SPI模式,中断模式,查询模式等SCK选择,主从 ...
- Android.mk文件详解(转)
源:Android.mk文件详解 从对Makefile一无所知开始,折腾了一个多星期,终于对Android.mk有了一个全面些的了解.了解了标准的Makefile后,发现Android.mk其实是把真 ...
- eclipse控制台中文乱码解决
Eclipse控制台中的中文输出乱码问题 博客分类: MySql EclipseTomcatXMLWeb 今天做S2SH集成的例子,所有该设置的地方都设置成了UTF-8,包括tomcat的配置文件s ...
- iOS UIActivityIndicatorView 的使用
UIActivityIndicatorView 非常简单 ,就是一个转圈圈的控件:http://blog.csdn.net/zhaopenghhhhhh/article/details/1209265 ...
- make 要点简记
make 要点简记 1.隐式推导 make可以自动推导文件及其文件依赖关系后面的命令,所以我们没有必要在每一个.o文件后面都写上类似的命令,因为make 会自动识别并且自动推导命令. objects ...
- Asp.Net集群中Session共享
今天遇到了这个问题,于是研究了一下.要解决这个问题,首先就要明白一些Session的机理.Session在服务器是以散列表形式存在的,我们都知道Session是会话级的,每个用户访问都会生成一个Ses ...