POJ2185-Milking Grid(KMP,next数组的应用)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 6317 | Accepted: 2648 |
Description
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
* 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
Sample Input
2 5
ABABA
ABABA
Sample Output
2
#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数组的应用)的更多相关文章
- POJ2185 Milking Grid KMP两次(二维KMP)较难
http://poj.org/problem?id=2185 大概算是我学KMP简单题以来最废脑子的KMP题目了 , 当然细节并不是那么多 , 还是码起来很舒服的 , 题目中描写的平铺是那种瓷砖一 ...
- [USACO2003][poj2185]Milking Grid(kmp的next的应用)
题目:http://poj.org/problem?id=2185 题意:就是要求一个字符矩阵的最小覆盖矩阵,可以在末尾不完全重合(即在末尾只要求最小覆盖矩阵的前缀覆盖剩余的尾部就行了) 分析: 先看 ...
- poj2185 Milking Grid【KMP】
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10084 Accepted: 4371 Des ...
- POJ2185 Milking Grid 【lcm】【KMP】
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- POJ 2185 Milking Grid KMP(矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Lim ...
- Poj 2165 Milking Grid(kmp)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Description Every morning when they are milked, ...
- POJ 2185 Milking Grid [KMP]
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8226 Accepted: 3549 Desc ...
- POJ 2185 Milking Grid KMP循环节周期
题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...
- 【kmp算法】poj2185 Milking Grid
先对每行求出所有可能的循环节长度(不需要整除). 然后取在所有行中都出现了的,且最小的长度为宽. 然后将每一行看作字符,对所有行求next数组,将n-next[n](对这些行来说最小的循环节长度)作为 ...
- POJ2185 Milking Grid 题解 KMP算法
题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...
随机推荐
- [典型漏洞分享]结合YS业务分析使用oauth协议的风险
结合YS业务分析oauth协议风险 问题描述: YS 使用QQ互联的openAPI实现QQ登录YS的功能,使用该功能需要在腾讯注册登录时的回调地址,根据oauth协议,用户的code或者access_ ...
- rust 参考的资料 转
http://blog.csdn.net/loveisasea/article/details/46292715 rust官方学习文档: 1.http://doc.rust-lang.org/book ...
- CSS3技巧:fit-content水平居中
当我们让一个模块水平居中首先想到的肯定是margin:0 auto;有木有?那么今天给大家介绍一个fit-content属性,不知道有没有同学用过,如果用过那么你可以略过这篇文章,没用过的同学就继续了 ...
- 翻译:Spring-Framework-Reference Document:15.2-DispatcherServlet
写在前面的话: 最近被项目的代码折腾的死去活来的,其实框架也没有那么难理解,只是自己的Web基础太差,被Request和Response这一对神雕侠侣坑到泪流满面!今天捣腾了一下Spring We ...
- Windows下生成 公钥 私钥以及 配置 Filezilla中的 SFTP的私钥
Win下需要使用到 PuTTYgen.exe来生成公钥私钥,可以参考youtube的这篇文章: 为 SFTP 保管箱生成 Secure Shell (SSH) 密钥对 PuTTYgen.exe的下载 ...
- synchronized 线程同步-类级别锁定
1.demo 说明:代码中通过 printNum 方法传入参数判断 a.b 分别对 num 这个参数的值进行了修改. package demo1; import sun.applet.Main; pu ...
- 微软对外披露两个0day漏洞详情
微软近日对外披露了两个0day漏洞详情,其中一个漏洞存在Adobe阅读器中,可被利用导致任意代码执行:另一个漏洞则允许任意代码在Windows kernel内存中提权执行. 微软称由于该漏洞利用目前还 ...
- vue项目如何实现剪切板功能--vue-clipboard2
一.vue项目利用vue-clipboard2实现剪切板的功能 1.安装vue-clipboard2插件:cnpm install --save vue-clipboard2 2.main.js添加 ...
- Apache2.4 与 php7.1.6的链接
首先Apache已经安装成功,在浏览器中能够打开再下载php 我的Apache安装版本为Apache2.4.26 x64 vc14 所以我php也应该是vc14编译的 php下载地址为 http:// ...
- shell遍历文件夹并执行命令
背景: 有一个源码包里面包含很多子目录和makefile,打包后的压缩包太大,需要将make生成的所有二进制文件删除然后再打包. 需求: 因此,要求在制定目录的所有递归子目录中执行make clean ...