Milking Grid
poj2185:http://poj.org/problem?id=2185
题意:在一个字符矩阵中,找一个最小的字符子矩阵,使其能够覆盖整个矩阵。
题解:在KMP中i-next[i]是这能够覆盖这个串的最小串长度。所以这一题可以用KMP搞。对于每一行求一个最小覆盖,然后取其最小公倍数,如果最大的那个覆盖的2倍不小于串的的长度,那么此时应该取这个长度。然后列也是这个处理,最后把两个数乘起来就是要找的面积。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 10005
using namespace std;
int next[N];
int n,m;
char s1[N];//模板串
char s2[N];//主串
int len1,len2;
int ans1,ans2;
char str1[N][];
void getnext(int plen){
int i = ,j = -;
next[]=-;
while( i<plen ){
if(j==-||s1[i]==s1[j]){
++i;++j;
// if(s1[i]!=s1[j] )
next[i] = j;
// else
// next[i] = next[j];
}
else
j = next[j];
}
}
int gcd(int a, int b){
if(b==)
return a;
return gcd(b,a%b);
} int lcm(int a, int b){//两个数的最小公倍数
return a*b/gcd(a, b);
}
int main(){
while(~scanf("%d%d",&n,&m)){
ans1=;
ans2=;
int maxn=;
for(int i=;i<=n;i++){
scanf("%s",s1);
strcpy(str1[i],s1);
getnext(m);
// printf("**%d\n",m-next[m]);
maxn=max(maxn,m-next[m]);
ans1=lcm(ans1,m-next[m]);
}
for(int i=;i<m;i++){
for(int j=;j<=n;j++){
s1[j-]=str1[j][i];
}
getnext(n); ans2=lcm(ans2,n-next[n]);
}
if(maxn*>=m)ans1=maxn;
else if(ans1>m)ans1=m;
if(ans2>n)ans2=n;
printf("%d\n",ans1*ans2);
}
}
Milking Grid的更多相关文章
- POJ 2185 Milking Grid KMP(矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Lim ...
- POJ 2185 Milking Grid(KMP)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4738 Accepted: 1978 Desc ...
- 【POJ2185】【KMP + HASH】Milking Grid
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 Limit: 65536K Total Submissions: 8226 Accepted: 3549 Desc ...
- poj 2185 Milking Grid
Milking Grid http://poj.org/problem?id=2185 Time Limit: 3000MS Memory Limit: 65536K Descript ...
- poj2185 Milking Grid【KMP】
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10084 Accepted: 4371 Des ...
- CH1808 Milking Grid
题意 POJ2185 数据加强版 描述 Every morning when they are milked, the Farmer John's cows form a rectangular gr ...
- POJ2185 Milking Grid 【lcm】【KMP】
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- AC日记——Milking Grid poj 2185
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8314 Accepted: 3586 Desc ...
- POJ 2185 Milking Grid [二维KMP next数组]
传送门 直接转田神的了: Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6665 Accept ...
随机推荐
- JS 操作 HTML 自定义属性
<input type="text" id="txtBox" displayName="123456" /> 获取自定义属性值: ...
- [转] [翻译]图解boost::bind
http://kelvinh.github.io/blog/2013/12/03/boost-bind-illustrated/ 其实这是很久之前留的一个坑了,一直没有填.. 记得在刚开始看到 boo ...
- Java基础知识强化之IO流笔记19:FileOutputStream的三个write方法
1. FileOutputStream的三个write方法: void write(byte[] buffer) Writes the entire contents of th ...
- visual studio 2015 删除空行 ,缩进css
查找 ^(?([^\r\n])\s)*\r?$\r?\n
- .NET读取Excel
1.代码 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Ext ...
- 使用Cxf发布Webservice服务,如果待发布的接口中有重载方法,怎么处理??
使用 @WebMethod(operationName="multiParamByName") 重新指定名字. http://bbs.csdn.net/topics/270059 ...
- form表单中的label标签
小伙伴们,你们在前面学习表单各种控件的时候,有没有发现一个标签--label,这一小节就来揭晓它的作用. label标签不会向用户呈现任何特殊效果,它的作用是为鼠标用户改进了可用性.如果你在 labe ...
- Linux2.6的所有内核版本
Index of /pub/linux/kernel/v2.6 Name Last modified Size Parent Directory - incr/ 03-Aug-2011 20:47 - ...
- 【USACO 2.2.3】循环数
[题目描述] 循环数是那些不包括0且没有重复数字的整数(比如81362)并且还应同时具有一个有趣的性质, 就像这个例子: 如果你从最左边的数字开始(在这个例子中是8)向右数最左边这个数(如果数到了最右 ...
- MySQL 缓存 Query Cache
QueryCache(下面简称QC)是根据SQL语句来cache的.一个SQL查询如果以select开头,那么MySQL服务器将尝试对其使 用QC.每个Cache都是以SQL文本作为key来存的.在应 ...