zoukankan      html  css  js  c++  java
  • poj 2185 Milking Grid

    Milking Grid
    Time Limit: 3000MS   Memory Limit: 65536K
         

    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字符矩阵中找出一个最小子矩阵,使其多次复制所得的矩阵包含原矩阵,输出最小矩阵面积
    样例解释:多次复制 AB
    解法:KMP
    将矩阵每一行看做一个单位,对这n个单位哈希后做KMP,求出最短循环节len1
    将矩阵每一列看做一个单位,对这m个单位哈希后做KMP,求出最短循环节len2
    答案=len1*len2
    #include<cstdio>
    #include<iostream>
    #define scale 26
    using namespace std;
    int n,m;
    int a[10001][76];
    long long horizontal[10001],vertical[77];
    int fh[10011],fv[81];
    void kmp()
    {
    	for(int i=1;i<n;i++)
    	{
    		int j=fh[i];
    		while(j&&horizontal[j]!=horizontal[i]) j=fh[j];
    		fh[i+1]= horizontal[i]==horizontal[j] ? j+1 : 0 ;
    	}
    	for(int i=1;i<m;i++)
    	{
    		int j=fv[i];
    		while(j&&vertical[i]!=vertical[j]) j=fv[j];
    		fv[i+1]= vertical[i]==vertical[j] ? j+1 : 0 ;
    	}
    }
    int main()
    {
    	scanf("%d%d",&n,&m);
    	char c;
    	for(int i=1;i<=n;i++)
    	 for(int j=1;j<=m;j++)
    	 {
    	 	cin>>c;
    	 	a[i][j]=c-'A'+1;
    	 	horizontal[i-1]=horizontal[i-1]*scale+a[i][j];
    	 	vertical[j-1]=vertical[j-1]*scale+a[i][j];
    	 }
    	kmp();
    	printf("%d",(n-fh[n])*(m-fv[m]));
    }
    

      

  • 相关阅读:
    App Store 审核指南
    Redis持久化
    PHP扩展高性能日志系统SeasLog简单上手
    Linux下Redis的安装配置
    Windows下Redis的安装
    安装wamp环境 最新完整版
    Git安装配置(完整版)
    Linux下SVN配置
    配置最新版LAMP环境
    Linux下ftp的安装配置
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6668712.html
Copyright © 2011-2022 走看看