POJ 2185 Milking Grid [KMP]
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 8226 | Accepted: 3549 |
Description
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
Hint
Source
题意:在N*M字符矩阵中找出一个最小子矩阵,使其多次复制所得的矩阵包含原矩阵。N<=10000,M<=75
只需要一行做一个字母求一次,再一列做一个字母求一次就好了,然后和子串是一样的n-fail[n]
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=1e4+,M=;
int n,m,k,fail[N];
char s[N][M];
bool cmp1(int a,int b){
for(int i=;i<=m;i++) if(s[a][i]!=s[b][i]) return false;
return true;
}
bool cmp2(int a,int b){
for(int i=;i<=k;i++) if(s[i][a]!=s[i][b]) return false;
return true;
}
void getFail(int n,bool (*cmp)(int a,int b)){
fail[]=;
for(int i=;i<=n;i++){
int j=fail[i-];
while(j&&!cmp(j+,i)) j=fail[j];
fail[i]=cmp(j+,i)?j+:;
}
} int main(){
// freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%s",s[i]+);
getFail(n,cmp1);
k=n-fail[n];
memset(fail,,sizeof(fail));
getFail(m,cmp2);
printf("%d",k*(m-fail[m]));
}
POJ 2185 Milking Grid [KMP]的更多相关文章
- POJ 2185 Milking Grid KMP循环节周期
题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...
- POJ 2185 Milking Grid KMP(矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Lim ...
- [poj 2185] Milking Grid 解题报告(KMP+最小循环节)
题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...
- POJ 2185 Milking Grid(KMP)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4738 Accepted: 1978 Desc ...
- POJ 2185 Milking Grid [二维KMP next数组]
传送门 直接转田神的了: Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6665 Accept ...
- 题解报告:poj 2185 Milking Grid(二维kmp)
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- poj 2185 Milking Grid
Milking Grid http://poj.org/problem?id=2185 Time Limit: 3000MS Memory Limit: 65536K Descript ...
- 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,求最小覆盖子矩阵,好题)
题意:给出一个大矩阵,求最小覆盖矩阵,大矩阵可由这个小矩阵拼成.(就如同拼磁砖,允许最后有残缺) 正确解法的参考链接:http://poj.org/showmessage?message_id=153 ...
随机推荐
- ES5.0集群搭建
最近在网上看到很多ES集群的搭建方法,本人在这人使用Elasticsearch5.0.1版本,介绍如何搭建es集群并安装head插件和其他插件安装方法. 一.搭建环境(2台Centos7系统服务器) ...
- Linux6.X图形界面如何打开终端以及如何将终端加入右键
今天刚安装了一个centos 6.9图形界面的系统,安装完成后,鼠标右击没有打开终端的按钮,在网上查了一些资料,搞明白了,分享给大家. 在左上角菜单[Applications]--->[Syst ...
- github网站介绍、并使用git命令管理github(详细描述)
本章学习: 1)熟悉github网站 2)通过git命令远程管理github, 3)git命令使用ssh key密钥无需输入账号密码 1.首先我们来熟悉github网站 1.1 注册github 登录 ...
- ZooKeeper 分布式共享锁的实现
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/8352919.html ------------------------------------ ...
- LNMP环境的搭建
http://blog.csdn.net/wzy_1988/article/details/8438355#
- drawpoly()函数的用法
画多边形的函数drawpoly() 用当前绘图色.线型及线宽,画一个给定若干点所定义的多边形.第一个参数,是多边形的顶点数第二个参数,是该数组中是多边形所有顶点(x,y)坐标值,即一系列整数对
- JAVA正则表达式 Pattern和Matcher
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 1.简介: java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. ...
- java中的左右移
package scanner; public class LeftMove { public static void main(String[] args) { int i = 1; System. ...
- oracle01
01-基本的查询语句 A:一些命令 sqlplus sys/sys@192.168.22.220:1521/orcl as sysdba; conn scott/123456@192.168.22.2 ...
- 深入浅出讲解:php的socket通信[转]
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1. 什么是TCP/IP.UDP?2. Sock ...