zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    2010年10月全球web服务器调查
    Orchard 发布1.0版本和ASP.NET MVC 3 RTM
    推荐一个在Linux/Unix上架设ASP.NET的 WEB服务器Jexus
    Windows Azure 的成本架构
    RavenDB系列教程
    Mono环境下不支持中文的解决方法
    IIS 7.0的六大安全新特性为你的Web服务器保驾护航
    Windows 7 Phone 文档数据库Rapid Repository正式发布
    使用log4Net 输出日志到mongodb
    Ubuntu & Fedora Mono 2.8 安装脚本
  • 原文地址:https://www.cnblogs.com/dream-maker-yk/p/9769468.html
Copyright © 2011-2022 走看看