Milking Grid
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 6317   Accepted: 2648

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
题意:r*c的字符串,问用最小的面积的字符串去覆盖它。求最小的面积
思路:能够分行分列考虑,easy想到当仅仅考虑行的时候,仅仅要把每一行看成一个字符,就能够求出关于行的next数组,然后求出最短的循环串 r-next[r] ,列也是如此,所以终于答案就是 (c-P[c])*(r-F[r]) P,F分别为各自的next数组。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 10000+10;
const int maxm = 80;
char mat[maxn][maxm];
char revmat[maxm][maxn];
int r,c;
int P[maxn],F[maxn];
int gcd(int a,int b) {
if(b==0) return a;
else return gcd(b,a%b);
}
void getP() {
P[1] = P[0] = 0;
for(int i = 1; i < r; i++) {
int j = P[i];
while(j && strcmp(mat[i],mat[j])) j = P[j];
if(strcmp(mat[i],mat[j])==0) P[i+1] = j+1;
else P[i+1] = 0;
}
}
void getF() {
F[1] = F[0] = 0;
for(int i = 1; i < c; i++) {
int j = F[i];
while(j && strcmp(revmat[i],revmat[j])) j = F[j];
if(strcmp(revmat[i],revmat[j])==0) F[i+1] = j+1;
else F[i+1] = 0;
}
}
void getRev() {
for(int i = 0; i < c; i++) {
for(int j = 0; j < r; j++) {
revmat[i][j] = mat[j][i];
}
}
}
void solve() {
int L = r-P[r],R = c - F[c];
printf("%d\n",L*R);
}
int main(){ while(~scanf("%d%d",&r,&c)){
for(int i = 0; i < r; i++) scanf("%s",mat[i]);
getP();
getRev();
getF();
solve();
}
return 0;
}

POJ2185-Milking Grid(KMP,next数组的应用)的更多相关文章

  1. POJ2185 Milking Grid KMP两次(二维KMP)较难

    http://poj.org/problem?id=2185   大概算是我学KMP简单题以来最废脑子的KMP题目了 , 当然细节并不是那么多 , 还是码起来很舒服的 , 题目中描写的平铺是那种瓷砖一 ...

  2. [USACO2003][poj2185]Milking Grid(kmp的next的应用)

    题目:http://poj.org/problem?id=2185 题意:就是要求一个字符矩阵的最小覆盖矩阵,可以在末尾不完全重合(即在末尾只要求最小覆盖矩阵的前缀覆盖剩余的尾部就行了) 分析: 先看 ...

  3. poj2185 Milking Grid【KMP】

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

  4. POJ2185 Milking Grid 【lcm】【KMP】

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

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

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

  6. Poj 2165 Milking Grid(kmp)

    Milking Grid Time Limit: 3000MS Memory Limit: 65536K Description Every morning when they are milked, ...

  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 KMP循环节周期

    题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...

  9. 【kmp算法】poj2185 Milking Grid

    先对每行求出所有可能的循环节长度(不需要整除). 然后取在所有行中都出现了的,且最小的长度为宽. 然后将每一行看作字符,对所有行求next数组,将n-next[n](对这些行来说最小的循环节长度)作为 ...

  10. POJ2185 Milking Grid 题解 KMP算法

    题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...

随机推荐

  1. POJ 2187 Beauty Contest (求最远点对,凸包+旋转卡壳)

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 24283   Accepted: 7420 D ...

  2. win32 打印机api

    ? 4. API之打印函数 AbortDoc 取消一份文档的打印 AbortPrinter 删除与一台打印机关联在一起的缓冲文件 AddForm 为打印机的表单列表添加一个新表单 AddJob 用于获 ...

  3. appium+python自动化44-appium命令行模式

    前言 appium desktop有个客户端版本,每次运行脚本的时候都要去双击启动才能运行,很显然不太方便,影响效率.那么有没什么办法不启动桌面程序就能运行呢,比如cmd命令行执行? 环境: appi ...

  4. 在 Android Studio 2.2 中愉快地使用 C/C++

    转载请注明出处:http://blog.csdn.net/wl9739/article/details/52607010 注:官网上面的技术文章也在不断地汉化中,只是进度有点慢.在我翻译本篇文章的时候 ...

  5. soap-学习

    1. SOAP 是一种简单的基于 XML 的协议,它使应用程序通过 HTTP 来交换信息. 简单的说:SOAP是用于访问网络服务的协议. 2. 什么是SOAP SOAP 指简易对象访问协议 SOAP ...

  6. 用google mock模拟C++对象

    google mock是用来配合google test对C++项目做单元测试的.它依赖于googletest(参见我上篇文章<如何用googletest写单元测试>: http://blo ...

  7. window进入本地组策略编辑器和计算机管理界面

    win+R 进入运行窗口 输入gpedit.msc 进入本地组策略编辑器, 右键我的电脑,点击管理 进入计算机管理界面

  8. SQL通过身份证获取信息

    SELECT t.identity_number '身份证号',SUBSTR(t.identity_number,1,2) AS "省份",SUBSTR(t.identity_nu ...

  9. Set 遍历的三种方法

    1.迭代遍历:Set<String> set = new HashSet<String>();Iterator<String> it = set.iterator( ...

  10. AWS RDS mysql无法连接的问题

    rds创建后,无法连接mysql 检查安全组规则是否配置了 1. 2. 这样你的EC2就可以访问了.如果还不行,检查数据库是否和EC2在同一 VPC内. 官方文档:https://docs.amazo ...