隐藏页面特效

POJ2185 Milking Grid

Milking Grid
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8248   Accepted: 3557

Description

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. 

Input

* 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. 

Output

* Line 1: The area of the smallest unit from which the grid is formed 

Sample Input

2 5 ABABA ABABA

Sample Output

2

Hint

The entire milking grid can be constructed from repetitions of the pattern 'AB'.

Source

 

题意:

  在N*M字符矩阵中找出一个最小子矩阵,使其多次复制所得的矩阵包含原矩阵。N<=10000,M<=75
做法:
  先找出最大的K,使得原矩阵是若干个K*M的矩阵拼成一列后的子矩阵
  把一行看做一个整体,对列做KMP
  用应用1的方法确定最小行宽
  再在K*M的矩阵中,把一列看做一个整体,用同样的方法求最小行宽
  O(N*M)
 
 
#include<cstdio> #include<cstring> using namespace std; const int N=1e4+5; const int M=80; char s[N][M]; int n,m; int l1,repetend1,fail1[N]; int l2,repetend2,fail2[M]; long long h1[N],h2[M]; void get_next1(){ int p=0;fail1[1]=0; for(int i=2;i<=l1;i++){ while(p>0&&h1[i]!=h1[p+1]) p=fail1[p]; if(h1[i]==h1[p+1]) p++; fail1[i]=p; } repetend1=l1-fail1[l1]; } void get_next2(){ int p=0;fail2[1]=0; for(int i=2;i<=l2;i++){ while(p>0&&h2[i]!=h2[p+1]) p=fail2[p]; if(h2[i]==h2[p+1]) p++; fail2[i]=p; } repetend2=l2-fail2[l2]; } #define P 29 int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",s[i]+1); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ h1[i]=h1[i]*P+s[i][j]; } } for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ h2[i]=h2[i]*P+s[j][i]; } } l1=n;get_next1(); l2=m;get_next2(); printf("%d",repetend1*repetend2); return 0; }

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/6382910.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(561)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示