设为首页 加入收藏

TOP

POJ2185-Milking Grid(KMP,next数组的应用)
2015-07-20 17:46:12 来源: 作者: 【 】 浏览:2
Tags:POJ2185-Milking Grid KMP next 应用
Milking Grid
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 6317 Accepted: 2648

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
题意:r*c的字符串,问用最小的面积的字符串去覆盖它,求最小的面积 思路:可以分行分列考虑,容易想到当只考虑行的时候,只要把每一行看成一个字符,就可以求出关于行的next数组,然后求出最短的循环串 r-next[r] ,列也是如此,所以最终答案就是 (c-P[c])*(r-F[r]) P,F分别为各自的next数组。
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          using namespace std; const int maxn = 10000+10; const int maxm = 80; char mat[maxn][maxm]; char revmat[maxm][maxn]; int r,c; int P[maxn],F[maxn]; int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } void getP() { P[1] = P[0] = 0; for(int i = 1; i < r; i++) { int j = P[i]; while(j && strcmp(mat[i],mat[j])) j = P[j]; if(strcmp(mat[i],mat[j])==0) P[i+1] = j+1; else P[i+1] = 0; } } void getF() { F[1] = F[0] = 0; for(int i = 1; i < c; i++) { int j = F[i]; while(j && strcmp(revmat[i],revmat[j])) j = F[j]; if(strcmp(revmat[i],revmat[j])==0) F[i+1] = j+1; else F[i+1] = 0; } } void getRev() { for(int i = 0; i < c; i++) { for(int j = 0; j < r; j++) { revmat[i][j] = mat[j][i]; } } } void solve() { int L = r-P[r],R = c - F[c]; printf("%d\n",L*R); } int main(){ while(~scanf("%d%d",&r,&c)){ for(int i = 0; i < r; i++) scanf("%s",mat[i]); getP(); getRev(); getF(); solve(); } return 0; } 
        
       
      
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇codeforces T-primes 230 B 素数.. 下一篇poj 1200 (hash)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言中如何将结构体 (2025-12-24 22:20:09)
·纯C语言结构体成员变 (2025-12-24 22:20:06)
·C语言中,指针函数和 (2025-12-24 22:20:03)
·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)