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 ...
随机推荐
- 找出单链表中倒数第K个元素
- c与c++d的typedef
一.基本概念剖析 int* (*a[5])(int, char*); //#1 void (*b[10]) (void (*)()); //#2 double(*)() (*pa)[9]; ...
- 让BLE设备的名称包含MAC地址
对于研发和测试BLE来说,经常看到同名的设备,是极为不方便的,一大堆设备同时上电会让同事不知道哪一个设备才是自己真正想操作的目标.再说一下小米手环,家中有三支小米手环,打开设备搜索全是“MI”,都不知 ...
- 为什么ios手机安装好fiddler证书/charles证书还是抓不到https请求?
为什么ios手机安装好fiddler证书/charles证书还是抓不到https请求? 最近有不少人有此困惑, 因为你的ios系统应该是10.0以上的系统, 在手机系统设置---关于手机----证书信 ...
- IntelliJ IDEA 2017.3下载与安装
大约在2017年暑假的时候知道了IntelliJ IDEA,但是那个时候一心认为有Eclipse就足够用了,然而今天在网上冲浪的时候发现,IntelliJ IDEA是java语言开发的集成环境,这款开 ...
- Web Component总结
Web Component 一个Web组件通常由四个部分组成:模板.Shadow DOM.自定义元素与打包,其中Shadow DOM解决了组件在页面中的封装问题 Shadow DOM 有shadow ...
- 学习JVM-GC收集器
1. 前言 在上一篇文章中,介绍了JVM中垃圾回收的原理和算法.介绍了通过引用计数和对象可达性分析的算法来筛选出已经没有使用的对象,然后介绍了垃圾收集器中使用的三种收集算法:标记-清除.标记-整理.标 ...
- 【分享】一个集成tracert和ping的网络监测工具
最近接到一个需求,需求背景是这样的:目前Windows平台下本身都有tracert和ping的实现,而且可以直接在cmd下使用. 需求中有两个要求: 1. Windows平台中的tracert执行速度 ...
- 织梦DEDECMS更换目录后页面内的图片和缩略图无法显示解决方法
http://www.win8f.com/seoyouhua/6609.html 很多人碰到织梦更换目录后内容图片和缩略图无法显示的问题,在此,慧鸿网络特地搜集整理了一篇关于织梦出现缩略图和内容无法显 ...
- OpenCV3.0 HDR(高动态范围)示例代码以及用法
OpenCV 3.0以及以后版本集成了HDR算法,样例代码的路径为: .\sources\samples\cpp\tutorial_code\photo\hdr_imaging.cpp. 实现算法的参 ...