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;
}

POJ:2185-Milking Grid(KMP找矩阵循环节)的更多相关文章

  1. POJ 2185 Milking Grid KMP(矩阵循环节)

                                                            Milking Grid Time Limit: 3000MS   Memory Lim ...

  2. POJ 2185 Milking Grid KMP循环节周期

    题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...

  3. POJ 2185 Milking Grid [KMP]

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8226   Accepted: 3549 Desc ...

  4. [poj 2185] Milking Grid 解题报告(KMP+最小循环节)

    题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...

  5. POJ 2185 Milking Grid [二维KMP next数组]

    传送门 直接转田神的了: Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6665   Accept ...

  6. POJ 2185 Milking Grid(KMP)

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4738   Accepted: 1978 Desc ...

  7. POJ 2185 Milking Grid(KMP最小循环节)

    http://poj.org/problem?id=2185 题意: 给出一个r行c列的字符矩阵,求最小的覆盖矩阵可以将原矩阵覆盖,覆盖矩阵不必全用完. 思路: 我对于字符串的最小循环节是这么理解的: ...

  8. 题解报告:poj 2185 Milking Grid(二维kmp)

    Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...

  9. poj 2185 Milking Grid

    Milking Grid http://poj.org/problem?id=2185 Time Limit: 3000MS   Memory Limit: 65536K       Descript ...

  10. Poj 2165 Milking Grid(kmp)

    Milking Grid Time Limit: 3000MS Memory Limit: 65536K Description Every morning when they are milked, ...

随机推荐

  1. PHP面试题及答案(五)

    1. 禁用COOKIE 后 SEESION 还能用吗? 答案: 不能. 2. 抓取远程图片到本地,你会用什么函数? 答案: fsockopen(). 3.求两个日期的差数,例如2007-2-5 ~ 2 ...

  2. 汇编语言版本的HelloWorld

    平台 macOS 工具 nasm clang 文件 main.asm extern _printf ; 这里调用系统的一个系统调用函数, _printf, 使用extern告诉链接器该label在其他 ...

  3. GridView相同内容合并单元格

    using System;using System.Data;using System.Configuration;using System.Collections;using System.Web; ...

  4. nodejs入门学习笔记一——一个完整的http路由服务实现

    开始学习nodejs! 参考书籍:The Node Beginner Book ,所有问题和讨论都围绕本书. 1.学习nodejs需要具备的基础知识: js基本语法,基本上写过前端的都能满足,原生js ...

  5. hibernate课程 初探单表映射1-2 ORM定义

    1 什么是ORM? ORM(Object / RelationShip Mapping) 对象/关系映射 面向对象编程(OOP)最终要把对象信息保存在关系性数据库中,要写好多sql语句.这与面向对象编 ...

  6. .net core +mysqlSugar(最为简单的增删改查)

    首先建立.net Core API - empty 这个就不说了 然后创建新的Controller 记得添加路由 [Route("api/Users")] 然后在Nuget Pac ...

  7. spring ehcache 使用详解

    Spring 整合 Ehcache 管理缓存详解  yellowbutterfly 前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象: ...

  8. JS案例练习-手机微信聊天对话框

    先附图 CSS部分: <style> body{} *{;} li{list-style: none;} .container{ width:310px; height:600px; ma ...

  9. newCoder在线编程---(1)

    二维数组查找 题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 1.暴 ...

  10. shell 快速浏览

    总结自: https://github.com/qinjx/30min_guides/blob/master/shell.md: http://blog.itpub.net/14293828/view ...