Milking Grid

POJ - 2185
时限: 3000MS   内存: 65536KB   64位IO格式: %I64d & %I64u

提交 状态

已开启划词翻译

问题描述

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.

输入

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

输出

* Line 1: The area of the smallest unit from which the grid is formed 

样例输入

  1. 2 5
  2. ABABA
  3. ABABA

样例输出

  1. 2

提示

The entire milking grid can be constructed from repetitions of the pattern 'AB'.

来源

kmp延伸……你是猪么
 
做两次KMP
行和列分别是len-next[len];
最后两个结果相乘就可以了
 
本题只是把线性变成平面,kmp对字符的操作变成字符串……
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6.  
  7. #define maxn 1000008
  8.  
  9. char s[maxn][];
  10. int r, c, next[maxn];
  11.  
  12. bool same1(int i, int j) // 判断第i行和第j行是否相等
  13. {
  14. for(int k = ; k < c; k++)
  15. if(s[i][k] != s[j][k])
  16. return false;
  17. return true;
  18. }
  19.  
  20. bool same2(int i, int j) // 判断第i列和第j列是否相等。
  21. {
  22. for(int k = ; k < r; k++)
  23. if(s[k][i] != s[k][j])
  24. return false;
  25. return true;
  26. }
  27.  
  28. int main()
  29. {
  30. while(~scanf("%d%d", &r, &c))
  31. {
  32. for(int i = ; i < r; i++)
  33. scanf("%s", s[i]);
  34. int j, k;
  35. memset(next, , sizeof(next));
  36. j = ;
  37. k = next[] = -;
  38. while(j < r)
  39. {
  40. while(- != k && !same1(j, k))
  41. k = next[k];
  42. next[++j] = ++k;
  43. }
  44. int ans1 = r - next[r]; // r-next[r]就是需要的最短的长度可以覆盖这个平面
  45. memset(next, , sizeof(next));
  46. j = ;
  47. k = next[] = -;
  48. while(j < c)
  49. {
  50. while(- != k && !same2(j, k))
  51. k = next[k];
  52. next[++j] = ++k;
  53. }
  54. int ans2 = c - next[c]; //列的
  55.  
  56. printf("%d\n", ans1*ans2);
  57. }
  58. return ;
  59. }
 
 

Milking Grid poj2185的更多相关文章

  1. 【POJ2185】【KMP + HASH】Milking Grid

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

  2. poj2185 Milking Grid【KMP】

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10084   Accepted: 4371 Des ...

  3. POJ2185 Milking Grid 【lcm】【KMP】

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

  4. CH1808 Milking Grid

    题意 POJ2185 数据加强版 描述 Every morning when they are milked, the Farmer John's cows form a rectangular gr ...

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

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

  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]

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

  8. poj 2185 Milking Grid

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

  9. AC日记——Milking Grid poj 2185

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8314   Accepted: 3586 Desc ...

随机推荐

  1. js 创建对象的方法

    <script> //1.字面量语法 var rectangle1 = {}; rectangle1.name="mindong"; rectangle1.width ...

  2. 解决旋转屏幕闪退在androidManifest.template.xml里,activity项添加:

     解决旋转屏幕闪退在androidManifest.template.xml里,activity项添加:android:configChanges="orientation|keyboard ...

  3. 20190925 On Java8 第二十二章 枚举

    第二十二章 枚举 基本 enum 特性 创建 enum 时,编译器会为你生成一个相关的类,这个类继承自 Java.lang.Enum. valueOf() 是在 Enum 中定义的 static 方法 ...

  4. IIS中配置Office COM组件 [dufu图解系列]

    解决问题: 读写office文档目前有2中方法,一种为引用微软的office com组件(下图为Microsoft Office 16.0 Object Library), 另一种为用第三方组件NPO ...

  5. Python3数据分析与挖掘建模实战 学习 教程

    Python3数据分析与挖掘建模实战 学习 教程 Python数据分析简介Python入门 运行:cmd下"python hello.py" 基本命令: 第三方库安装Windows ...

  6. [Python3 练习] 001 温度转换1

    题目:温度转换 I (1) 描述 温度的刻画有两个不同体系:摄氏度 (Celsius) 和华氏度 (Fabrenheit) 请编写程序将用户输入的华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度 转换 ...

  7. [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树)

    [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树) 题面 略 分析 首先把一组询问(x,y)看成二维平面上的一个点,我们想办法用数据结构维护这个二维平面(注意根据题意这 ...

  8. latex如何给表格添加注释

    在latex中,想给表格添加注释,可以使用threeparttable这个包 代码如下: \usepackage{threeparttable} \begin{table*} \begin{three ...

  9. Docker备份镜像

    docker save -o mycentos.tar mycentos_new:1.1 指定输出到的文件 执行后,运行 ls 命令即可看到打成的tar包, 因为有463M所以打包要一会

  10. CF 82 D.Two out of Three

    前言 全网唯一不同题解 设 \(f[i][j]\) 表示第 \(i\) 次选取留下来的数是 \(k\) 的最小花费 枚举前面的留下来的点 \(k\) 当前能留下的点只有 \((2*i),(2*i+1) ...