zoukankan      html  css  js  c++  java
  • POJ 2185 Milking Grid KMP(矩阵循环节)

                                                            Milking Grid
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 7153   Accepted: 3047

    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'.
     
    题意:找出矩阵中最小的子矩阵,满足循环它能形成包括原矩阵的矩阵
    题解:
           对于每一行,当作一个字符,跑kmp,若有hang[n]!=0 则有循环节,则行最短循环节长度=小矩阵的高=n-hang[n]
        对于列也是如此
    //作者:1085422276
    #include <cstdio>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    //#include<bits/stdc++.h>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    const int inf = 10000000;
    inline ll read()
    {
        ll x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    ll exgcd(ll a,ll b,ll &x,ll &y)
    {
        ll temp,p;
        if(b==0)
        {
            x=1;
            y=0;
            return a;
        }
        p=exgcd(b,a%b,x,y);
        temp=x;
        x=y;
        y=temp-(a/b)*y;
        return p;
    }
    //*******************************
    char mp[100002][770];
    int n,m,p[100005];
    int is_equal(int i,int j)
    {
        for(int k=0;k<m;k++)
        {
            if(mp[i][k]!=mp[j][k])return 0;
        }
        return 1;
    }
    int  is_equaf(int i,int j)
    {
        for(int k=0;k<n;k++)
        {
            if(mp[k][i]!=mp[k][j])return 0;
        }
        return 1;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
       {
              memset(p,0,sizeof(p));
           for(int i=0;i<n;i++)
           {
               scanf("%s",mp[i]);
           }
          // for(int i=1;i<=n;i++)cout<<mp[i]<<endl;
           //return 0;
           int j=0;
           for(int i=1;i<m;i++)
           {
               while(j&&!is_equaf(i,j))j=p[j];
               if(is_equaf(i,j))j++;
               p[i+1]=j;
           }
           int l;
           l=m-(p[m]);
           memset(p,0,sizeof(p));
           j=0;
           for(int i=1;i<n;i++)
           {
               while(j&&!is_equal(i,j))j=p[j];
               if(is_equal(i,j))j++;
               p[i+1]=j;
           }
           int r;
            r=n-(p[n]);
           cout<<l*r<<endl;
       }
        return 0;
    }
    代码
  • 相关阅读:
    metasploit模块功能介绍
    虚拟机端口映射
    linux 双网卡实现
    斯坦福第三课:线性代数回顾(Linear Algebra Review)
    斯坦福第二课:单变量线性回归(Linear Regression with One Variable)
    斯坦福第一课:引言(Introduction)
    机器学习的个人见解----深夜总结
    史上最强机器学习资料------来自个人心血总结-----5星级
    如何学习新的知识----心血总结
    21个必知数据科学面试题和答案
  • 原文地址:https://www.cnblogs.com/zxhl/p/4772398.html
Copyright © 2011-2022 走看看