Milking Grid poj2185
2024-09-05 03:46:34
POJ - 2185
时限: 3000MS | 内存: 65536KB | 64位IO格式: %I64d & %I64u |
已开启划词翻译
问题描述
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.
输入
* 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.
输出
* Line 1: The area of the smallest unit from which the grid is formed
样例输入
2 5
ABABA
ABABA
样例输出
2
提示
The entire milking grid can be constructed from repetitions of the pattern 'AB'.
来源
kmp延伸……你是猪么
做两次KMP
行和列分别是len-next[len];
最后两个结果相乘就可以了
本题只是把线性变成平面,kmp对字符的操作变成字符串……
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; #define maxn 1000008 char s[maxn][];
int r, c, next[maxn]; bool same1(int i, int j) // 判断第i行和第j行是否相等
{
for(int k = ; k < c; k++)
if(s[i][k] != s[j][k])
return false;
return true;
} bool same2(int i, int j) // 判断第i列和第j列是否相等。
{
for(int k = ; k < r; k++)
if(s[k][i] != s[k][j])
return false;
return true;
} int main()
{
while(~scanf("%d%d", &r, &c))
{
for(int i = ; i < r; i++)
scanf("%s", s[i]);
int j, k;
memset(next, , sizeof(next));
j = ;
k = next[] = -;
while(j < r)
{
while(- != k && !same1(j, k))
k = next[k];
next[++j] = ++k;
}
int ans1 = r - next[r]; // r-next[r]就是需要的最短的长度可以覆盖这个平面
memset(next, , sizeof(next));
j = ;
k = next[] = -;
while(j < c)
{
while(- != k && !same2(j, k))
k = next[k];
next[++j] = ++k;
}
int ans2 = c - next[c]; //列的 printf("%d\n", ans1*ans2);
}
return ;
}
最新文章
- SQLServer2000数据库的安装
- RadioButtonFor绑定值
- M2: XAML Controls(2)
- Java Web项目调优原则
- Test Android with QTP
- JavaScript自定义右键菜单
- 简单的JS运动封装实例---侧栏分享到
- port-channel和channel-group
- 浅析JavaScript引用类型之--Object、Array
- oracle12c:通过oracle客户端工具配置tns,并使用sqlldr进行批量导入数据
- Asp.Net Core&;Jenkins持续交付到Windows Server
- python下彻底解决浏览器多标签打开与切换问题
- Mosaic 前端微服务框架
- 由于想要实现下载的文件可以进行选择,而不是通过<;a>;标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载,但是ajax实现的文件下载并不能触发浏览器的下载文件弹出框,这里通过模拟表单提交实现同样的效果。
- Drools实战系列(三)之eclipse创建工程
- Finalize和Dispose的区别
- 回顾经典问题算法:LIS, LCS-(DP类别)
- 001 KNN分类 最邻近算法
- [译文]casperjs 的API-casper模块
- 007 Android 单击事件、toast使用