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 

样例输入

2 5
ABABA
ABABA

样例输出

2

提示

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

来源

USACO 2003 Fall
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][80];
10 int r, c, next[maxn];
11
12 bool same1(int i, int j)   // 判断第i行和第j行是否相等
13 {
14     for(int k = 0; 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 = 0; 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 = 0; i < r; i++)
33             scanf("%s", s[i]);
34         int j, k;
35         memset(next, 0, sizeof(next));
36         j = 0;
37         k = next[0] = -1;
38         while(j < r)
39         {
40             while(-1 != 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, 0, sizeof(next));
46         j = 0;
47         k = next[0] = -1;
48         while(j < c)
49         {
50             while(-1 != 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 0;
59 }

转载于:https://www.cnblogs.com/Tinamei/p/4803094.html

Milking Grid poj2185相关推荐

  1. POJ 2185 Milking Grid (KMP,GCD)

    http://poj.org/problem?id=2185 求最小覆盖子矩阵的面积,求出每行的最小覆盖子串,求最小公倍数,就是矩阵的长度 求出每列的最小覆盖子矩阵然后求最小公倍数,就是矩阵的宽 最小 ...

  2. linux6查看用户密码,linux如何查看所有的用户(user)、用户组(group)、密码(password/passwd)...

    linux如何查看所有的用户和组信息_百度经验 https://jingyan.baidu.com/article/a681b0de159b093b184346a7.html linux添加用户.用户 ...

  3. POJ的题目分类(两个版本)

    版本一: 简单题 1000A+B Problem 1001Exponentiation 1003 Hangover 1004 Financial Management 1005 I Think I N ...

  4. POJ前面的题目算法思路【转】

    1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...

  5. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

  6. 二维KMP - 求字符矩阵的最小覆盖矩阵 - poj 2185

    Milking Grid Problem's Link:http://poj.org/problem?id=2185 Mean: 给你一个n*m的字符矩阵,让你求这个字符矩阵的最小覆盖矩阵,输出这个最 ...

  7. POJ 超详细分类

    POJ 各题算法 1000    A+B Problem            送分题     49%    2005-5-7 1001    Exponentiation         高精度   ...

  8. 使用NVIDIA GRID vPC支持视频会议和算力工具

    使用NVIDIA GRID vPC支持视频会议和算力工具 随着2020年的发展,远程工作解决方案已成为许多人的新常态.企业正在寻找行之有效的解决方案,如虚拟桌面基础设施(VDI),以使他们的团队能够在 ...

  9. CSS grid 的用法

    grid 的用法 加三个宽度为 200px 的列. .container {display: grid;grid-template-columns: 200px 200px 200px; } 用 fr ...

最新文章

  1. javascript的知识总结
  2. matlab数据无量纲化_MATLAB数据预处理——归一化和标准化
  3. 如何写sybase sql脚本文件_写一个删除lock文件的skill脚本
  4. 解一元二次方程的C++实现
  5. tensorflow tf.matmul() (多维)矩阵相乘(多维矩阵乘法)
  6. GDCM:检测SIEMENS JPEG无损压缩图像的测试程序
  7. LogMiner学习笔记
  8. 搜索——Red and Black(hdu1312)
  9. 算法导论 第十三章 红黑树(python)-1插入
  10. teamviewer设备数量上限怎么解决_会议音响设备出现啸叫怎么办?不要担心,这3个方法帮你解决...
  11. 【链表】剑指offer:从尾到头打印链表
  12. 全才出书,值得一读——Leo推荐《我也能做CTO之程序员职业规划》
  13. OpenCV46:立体图像的深度图|Depth Map
  14. Mac 终端命令自动补齐的办法
  15. 2020-10-28-Requests及爬虫清单
  16. dedecms教程:龙书浩最新DedeCmsV5.7建站仿站VIP视频教程免费下载
  17. encode()和decode()编码与解码的解析、常用编码与为何要encode和decode
  18. 851-40亿个号码如何去重?
  19. Github上开源项目readme里好看的高大上的有趣的徽章从何而来
  20. 1018 锤子剪刀布python3无超时

热门文章

  1. Spring Boot log4j多环境日志级别的控制
  2. Hbase对时,时差范围的确定
  3. WdatePicker日历控件使用方法(转)
  4. 统计字符串中出现最多的单词和次多的单词
  5. .net分页控件webdiyer:AspNetPager
  6. div+css 简单导航
  7. vs2008保存超级慢
  8. 《天下无贼》经典对白
  9. python prettytable格式设置_Python prettytable的使用方法
  10. oracle11gr2架构图,Oracle 11g R2 体系结构