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;
    }
  • 相关阅读:
    sql server 笔记(数据类型/新建、修改、删除数据表/)
    在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: Nam
    jquery 操作 checkbox select
    layui常见问题
    Sublime Text 3下载-汉化-插件配置
    CSS前端开发学习总结、一
    如何用 JavaScript 下载文件
    腾讯大王卡、天王卡代申请
    新人报道~cnblogs
    Node.js
  • 原文地址:https://www.cnblogs.com/shenben/p/6382910.html
Copyright © 2011-2022 走看看