POJ2185 Milking Grid 【lcm】【KMP】

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,对每一行和每一列分开处理
一个字符串的最小循环节就是\(len - fail[len]\)
然后对所有行的最小循环节取lcm,对所有列的最小循环节取lcm
然后把这两个值乘起来就可以了


//Author: dream_maker
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
//fast read and write
template <typename T>
void Read(T &x) {
  bool w = 1;x = 0;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-') w = 0, c = getchar();
  while (isdigit(c)) {
    x = (x<<1) + (x<<3) + c -'0';
    c = getchar();
  }
  if (!w) x = -x;
}
template <typename T>
void Write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x; 
  }
  if (x > 9) Write(x / 10);
  putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 10010;
char s1[N][80], s2[80][N];
int fail1[N][80], fail2[N][80];
int n, m;
int gcd(int a, int b) {
  return b ? gcd(b, a%b) : a;
}
int lcm(int a, int b) {
  return a / gcd(a, b) * b;
}
void getfail(char *s, int *fail, int len) {
  fail[1] = 0;
  int j = 0;
  fu(i, 2, len) {
    while (j && s[j + 1] != s[i]) j = fail[j];
    if (s[i] == s[j + 1]) ++j;
    fail[i] = j;
  }
}
int main() {
#ifdef dream_maker
  freopen("input.txt", "r", stdin);
#endif
  Read(n), Read(m);
  fu(i, 1, n) {
    fu(j, 1, m)
      s1[i][j] = s2[j][i] = getchar();
    getchar();
  }
  int tmpn = 1;
  fu(i, 1, n) {
    getfail(s1[i], fail1[i], m);
    tmpn = lcm(tmpn, m - fail1[i][m]);
    if (tmpn >= m) {
      tmpn = m;
      break;
    }
  }
  int tmpm = 1;
  fu(i, 1, m) {
    getfail(s2[i], fail2[i], n);
    tmpm = lcm(tmpm, n - fail2[i][n]);
    if (tmpm >= n) {
      tmpm = n;
      break;
    }
  }
  Write(tmpn * tmpm);
  return 0;
}

转载于:https://www.cnblogs.com/dream-maker-yk/p/9769468.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值