POJ:2185-Milking Grid(KMP找矩阵循环节)

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’.


解题心得:

  1. 题意就是给你一个字符矩阵,叫你找出这个字符矩阵的循环节(子矩阵)的大小,给你的字符矩阵可能只是给你一部分。
  2. 刚开始在看到这个题的时候瞬间想到了每一行用KMP找循环节,然后求最小公倍数,然后WA,闷了好久才反应过来为啥只在行里面找,每一列也需要用KMP找寻环节得到列的最小公倍数,然后行列的最小公倍数乘起来才是最小循环矩阵的面积啊。但是要注意如果行或者列的最小公倍数比给出的n,m更大的时候要选择n,m,就是以自身为一个循环节
  3. 注意行列的大小,开数组的时候不要开反了,RE到怀疑人生。

#include<stdio.h>
#include<math.h>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn_length = 1e4+100;
const int maxn_wide = 100;
char maps[maxn_length][maxn_wide];
ll n,m,next[maxn_length];

ll __gcd(ll a,ll b)
{
    if(b == 0)
        return a;
    return __gcd(b,a%b);
}

ll kmp_len(ll pos)//每一行看毛片
{
    ll k = -1;
    memset(next,-1,sizeof(next));
    for(int i=1;i<m;i++)
    {
        while(k > -1 && maps[pos][i] != maps[pos][k+1])
            k = next[k];
        if(maps[pos][i] == maps[pos][k+1])
            k++;
        next[i] = k;
    }
    return m-1-next[m-1];
}

ll get_lcm_length()//得到每一行的最小公倍数
{
    ll ans = 1,cnt;
    for(int i=0;i<n;i++)
    {
        ll len = kmp_len(i);
        cnt = ans/__gcd(len,ans)*len;
        ans = cnt;
        if(ans > m)
            return m;
    }
    return ans;
}

ll kmp_wide(ll pos)//每一列看毛片
{
    ll k = -1;
    memset(next,-1,sizeof(next));
    for(int i=1;i<n;i++)
    {
        while(k > -1 && maps[i][pos] != maps[k+1][pos])
            k = next[k];
        if(maps[i][pos] == maps[k+1][pos])
            k++;
        next[i] = k;
    }
    return n-1-next[n-1];
}

ll get_lcm_wide()//得到每一列的最小公倍数
{
    ll ans = 1,cnt;
    for(int i=0;i<m;i++)
    {
        ll wide = kmp_wide(i);
        cnt = ans/__gcd(ans,wide)*wide;
        ans = cnt;
        if(ans > n)
            return  n;
    }
    return ans;
}

int main()
{
    while(cin>>n>>m)
    {
        for(int i=0;i<n;i++)
            scanf("%s",maps[i]);
        ll lcm_len = get_lcm_length();
        ll lcm_wide = get_lcm_wide();
        lcm_len = min(lcm_len,(ll)m);
        lcm_wide = min(lcm_wide,(ll)n);

        ll ans = lcm_len * lcm_wide;
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值