POJ 2185 Milking Grid

Milking Grid

Time Limit: 3000ms

Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 2185
64-bit integer IO format: %lld      Java class name: Main

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,两次KMP,求最小循环节的乘积。

Source

USACO 2003 Fall

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 int row,col,fail[10100],x,y;
18 char str[10010][80];
19 bool cmp(int a,int b){
20     for(int i = 0; i < row; i++)
21         if(str[i][a] != str[i][b]) return false;
22         return true;
23 }
24 int main() {
25     while(~scanf("%d %d",&row,&col)){
26         for(int i = 0; i < row; i++)
27             scanf("%s",str[i]);
28         fail[0] = fail[1] = 0;
29         for(int i = 1; i < row; i++){
30             int j = fail[i];
31             while(j && strcmp(str[i],str[j])) j = fail[j];
32             if(!strcmp(str[i],str[j])) fail[i+1] = j+1;
33             else fail[i+1] = 0;
34         }
35         x = row - fail[row];
36         fail[0] = fail[1] = 0;
37         for(int i = 1; i < col; i++){
38             int j = fail[i];
39             while(j && !cmp(i,j)) j = fail[j];
40             if(cmp(i,j)) fail[i+1] = j+1;
41             else fail[i+1] = 0;
42         }
43         y = col - fail[col];
44         printf("%d\n",x*y);
45     }
46     return 0;
47 }

时间: 2024-10-24 08:14:19

POJ 2185 Milking Grid的相关文章

POJ 2185 Milking Grid KMP循环节周期

题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一个AB 组成一个 ABABAB ABABAB 能够多出来 思路:每一行求出周期 总共n个 求这n个周期的最小公倍数 假设大于m 取m 每一列求出周期 总共m个求这个m个周期的最小公倍数 假设大于n取n 答案就是2个最小公倍数的积 #include <cstdio> #include <cst

poj 2185 Milking Grid(KMP)

题目链接:poj 2185 Milking Grid 题目大意:给定一个N?M的矩阵,找到一个最小的r?c的矩阵,使得原矩阵可以由若干个小矩阵组成,输出r?c的值. 解题思路:将行和列分别看成字符串,然后hash,对hash后的数组分别求KMP. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef unsigned long long ll; con

POJ 2185 Milking Grid (二维KMP next数组)

Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6665   Accepted: 2824 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

POJ 2185 Milking Grid(KMP)

解题思路: 算是一个多维的KMP,其实是一样的,不过把1个字符的比较改成一行或一列字符的比较,对行和列使用两次KMP,最后乘起来就可以了. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include &l

POJ 2185 Milking Grid (搬运KMP)

题意:给你一个字母矩阵,让你找一个最小的字母矩阵,复制后可以得到大的矩阵,问最小矩阵面积是多少,假设原来的矩阵长度是6,你用一个长度为5的串复制,多余的部分不算,只算6以内相同即可 思路:其实没有什么思路,看了一些大佬们的博客,其中有几篇写的很棒 传送门1,传送门2 大体的思路就是找到每行的循环节,然后插入在桶中,在另一侧记录的时候发现等于列的数量是就代表这是最小的(细节部分窝还要想几天) 代码: 不存在的(最近搬运代码越来越熟练了) 原文地址:https://www.cnblogs.com/l

POJ 2185 Milking Grid--另一种字符串的循环节

在Power String中,求一个字符串的循环节,应满足L mod (L-next[L])=0,则循环节长度为L-next[L] 存在另一种形式的循环节,例如abcabca,此时如果将abc重写三次,得到abcabcabc,则原字符串为其前缀. 此时对于原字符串,其循环节长度为L-next[L]=7-4=3,循环节为abc.具体可见下面这个题 [Baltic2009]Radio Transmission 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知

poj2185 Milking Grid(KMP运用)

题目链接:http://poj.org/problem?id=2185 Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6249   Accepted: 2616 Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that is R (1 <= R <= 1

Milking Grid poj2185

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.

[2016-03-28][POJ][3616][Milking Time]

时间:2016-03-28 17:27:03 星期一 题目编号:[2016-03-28][POJ][3616][Milking Time] #include <algorithm> #include <cstdio> using namespace std; const int maxm = 1000 + 10; struct Roo{ int l,r,v; bool operator < (const Roo & a)const{ return l < a.l