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 ...
随机推荐
- Spring的IOC分析(二)源码
承接上节继续,分析Ioc的工作原理,在典型的 IOC 场景中,容器创建了所有对象,并设置必要的属性将它们连接在一起(同时一个叫DI"依赖注入"或DL"依赖查找" ...
- CSS3技巧巧妙使用:not(:last-of-type)简化你的css代码
终于找到了一个好方法,使用:not(:last-of-type)简单方便,再也不要麻烦的单独使用:last-of-type了,不错! 应用场景:平时我们的列表一般都会有分割线,但是最后一个列表没有分割 ...
- MLlib--PIC算法
转载请标明出处http://www.cnblogs.com/haozhengfei/p/82c3ef86303321055eb10f7e100eb84b.html PIC算法 幂迭代聚类 ...
- 分布式文件系统FastDFS动态扩容
当用户量越来越大,则集群中某个group总会到达其极限,这时就得扩展集群的容量了. FastDFS的扩容分为对group纵向扩容和横向扩容 纵向扩容 指在同一个group组中增加服务器,实现数据冗余, ...
- 宝塔linux面板.txt
安装命令: yum -y install screen wget && screen -S bt wget -O install.sh http://103.224.251.79:58 ...
- dede 提交表单 发送邮件
第一步:要到dede后台设置好邮箱的资料,并且确定所用的邮箱开启了smtp 第二步:找到/plus/diy.php在 [cce]$query = "INSERT INTO `{$diy-&g ...
- .netCore数据库迁移
程序包管理器控制台下Nuget 命令: 初始迁移命令: add-migration init -Context DAL.ProductContext 全称:migrations add Initial ...
- Moogoose Constructor小坑
注意! exports 出来的 Model名字,必须和 Constructor的名字不一样!!! 不然Constructor会被覆盖,报错 这个是修改之后的.修改前,是var account = ne ...
- canvas实现倒计时效果示例(vue组件内编写)
前言: 此事例是在vue组件中,使用canvas实现倒计时动画的效果.其实,实现效果的逻辑跟vue没有关系,只要读懂canvas如何实现效果的这部分逻辑就可以了 canvas动画的原理:利用定时器,给 ...
- 使用WinDbg获取SSDT函数表对应的索引再计算得出地址
当从Ring3进入Ring0的时候会将所需要的SSDT索引放入到寄存器EAX中去,所以我们这里通过EAX的内容得到函数在SSDT中的索引号,然后计算出它的地址首先打开WinDbug,我们以函数ZwQu ...