zoukankan      html  css  js  c++  java
  • [poj 2185] Milking Grid 解题报告(KMP+最小循环节)

    题目链接:http://poj.org/problem?id=2185

    题目:

    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 
     

    题目大意:

    给出一个矩阵,求最小的可以通过复制包含原矩阵的矩阵大小

    题解:

    对每一行分别求最小循环节,答案矩阵的宽度就是这些最小循环节的lcm

    同理列也是这样处理

    
    
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #include<cstdio>
    using namespace std;
    
    const int N=1e4+15;
    int r,c,n,m;
    int next[N];
    char buf[N][80],tmp[N];
    int kmp(int l)
    {
        next[1]=0;
        for (int i=2,j=0;i<=l;i++)
        {
            while (j&&tmp[i]!=tmp[j+1]) j=next[j];
            if (tmp[i]==tmp[j+1]) j++;
            next[i]=j; 
        }
        return l-next[l];
    }
    int gcd(int x,int y)
    {
        if (!y) return x;
        return gcd(y,x%y);
    }
    int lcm(int x,int y)
    {
        return x/gcd(x,y)*y;
    }
    int main()
    {
        scanf("%d%d",&r,&c);
        for (int i=1;i<=r;i++)
        {
            scanf("%s",buf[i]+1);
        }
        int n=1,m=1;
        for (int i=1;i<=r;i++)
        {
            for (int j=1;j<=c;j++) tmp[j]=buf[i][j];
            n=lcm(n,kmp(c));
            if (n>c)
            {
                n=c;
                break;        
            }
        }
        for (int i=1;i<=c;i++)
        {
            for (int j=1;j<=r;j++) tmp[j]=buf[j][i];
            m=lcm(m,kmp(r));
            if (m>r)
            {
                m=r;
                break;
            }
        }
        printf("%d
    ",n*m);
        return 0;
    }



  • 相关阅读:
    基于Text-CNN模型的中文文本分类实战 流川枫 发表于AI星球订阅
    SQL Server 定时执行SQL语句的方法
    linq 根据指定条件返回集合中不重复的元素
    asp.net mvc ChildActionOnly 和ActionName的用法
    C# 让枚举返回字符串
    EF中使用SQL语句或存储过程
    Sql Server系列:视图
    C# 获取web.config配置文件
    C# 在EF中直接运行SQL命令
    c# mvc 获取 HtmlHelper 表达式值和时间格式化 去边框
  • 原文地址:https://www.cnblogs.com/xxzh/p/9709891.html
Copyright © 2011-2022 走看看