zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    web移动开发最佳实践之js篇
    ubuntu升级到12.10
    C语言生成随机数
    终于签约了
    这个2012不寻常
    awk练习(实战)
    数据恢复的教训
    职业发展的一些随想
    diy谷蜂Y5刷机包基于官方0207稳定版
    web移动开发最佳实践之html篇
  • 原文地址:https://www.cnblogs.com/shenben/p/6382910.html
Copyright © 2011-2022 走看看