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. synchronized和lock区别

    synchronized 快速回顾: 1.当代码块 加上 synchrozized之后,代码会发生什么改变? 答案:有两条改变.一个是原子性(atomicity),一个是可见性(visibility) ...

  2. express默认配置文件app.js

    Express路由 Express模块化路由 Express中间件 Express结合jade模板渲染HTML 看完上面的,再回头看这app.js,就应该感觉没什么压力了,主要包含http的创建,基本 ...

  3. [Eclipse插件] Eclipse中如何安装和使用GrepCode插件

    Java是开源的世界,如何快速的搜索到你需要的Java源码呢?2009年7月17日,GrepCode团队发布了一个有趣的 Java源码搜索引擎-GrepCode .与现有的各种搜索引擎相比,Java源 ...

  4. 如何破解linux用户帐号密码一

    ENCRYPT_METHOD SHA512 定义帐号密码的加密方式 1.第一步拿到散列,也就是加密后的密码hash值 2.可以去一些彩虹表(rainbow)网站查询这些hash对应的密码明文,稍微花些 ...

  5. 一、Hello Spring Boot

    package com.ld.controller; import org.springframework.web.bind.annotation.RequestMapping; import org ...

  6. pthread_join和pthread_detach的用法

    //从别处拷贝过来的,只作为自己查看方便,原作者不详,请谅解. 一:关于join join join是三种同步线程的方式之一.另外两种分别是互斥锁(mutex)和条件变量(condition vari ...

  7. [转载]Delphi 版 everything、光速搜索代码

    近日没啥事情,研究了一下 everything.光速搜索原理.花了一个礼拜时间,终于搞定. 废话不多说,直接上代码: unit uMFTSearchFile; { dbyoung@sina.com 2 ...

  8. URLRewrite地址重定向的实现

    URLRewrite就是我们通常说的地址重写,用户得到的全部都是经过处理后的URL地址.其优点有: (1)提高安全性,可以有效的避免一些参数名.ID等完全暴露在用户面前,如果用户随便乱输的话,不符合规 ...

  9. C# 获取父控件容器的属性

    C# 获取父控件容器的属性 BindingNavigator bindingNavigator = (sender as ToolStripButton).GetCurrentParent() as ...

  10. Android,TextView的所有属性和方法

    XML 属性 属性名称 相关方法 描述 android:autoLink setAutoLinkMask(int) 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接. ...