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 ...
随机推荐
- Linux Epoll介绍和程序实例
Linux Epoll介绍和程序实例 1. Epoll是何方神圣? Epoll但是当前在Linux下开发大规模并发网络程序的热门人选,Epoll 在Linux2.6内核中正式引入,和select类似, ...
- Firemonkey的旁门左道[五]
这次讲讲绘制的几种模式吧,不过还是比较浅显,刚接触不久,还实在没这个实力道出个所以来. FMX下,我们可以切换GDI,D2D,GPU这三种模式, 只要通过全局变量就可以轻松搞定. 如何设置 Globa ...
- select默认选择的实现方法
<script type="text/javascript"> <!-- document.biao.bbb.value = "云南";//b ...
- 使用XStream注解实现Java对象与XML互相转换的代码示例
本文记录一下使用xstream这个api的注解特性对Java对象与XML字符串相互转换的一些代码示例. 我们很多人都处理过XML文件,也有很多非常成熟的第三方开源软件.如:jdom.dom4j等 ...
- SCI科技论文写作技巧-核心价值
第一次写SCI论文写作技巧,本身不是大牛,也许没有资金格谈论这个. 这里仅仅是一些个人思考,不正确,好还是不好.而当另一种理论. 对于工程专业的学生,谁往往应用,书写SCI事情.当然,也不是没可能.全 ...
- centos 6+安装山逗斯骚尅特(本文内容来自都比更具帝)
系统支持:CentOS 6+,Debian 7+,Ubuntu 12+ 内存要求:≥128M 关于本脚本 一键安装 Shadowsocks-Python, ShadowsocksR, Shadowso ...
- Thinkphp 3.2及以上版本实现支付宝担保交易、即时到账接口类、函数和使用方法
给客户开发网站时需要用到支付宝在线付款功能,小云到thinkphp网站溜了一圈,代码是有,可是都不怎么全,因此这篇文章诞生了! 本篇文章讲解了三个类的实现,担保交易.即时到账.双功能收款(该功能支付宝 ...
- Python 代码实现模糊查询
Python 代码实现模糊查询 1.导语: 模糊匹配可以算是现代编辑器(如 Eclipse 等各种 IDE)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的文件名,并提供一个推荐列 ...
- android SDK开发 -- TitleBar封装(一)
假设app的title 统一的都是这种左中右结构的 代码如下 <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...
- poj 1125 Stockbroker Grapevine (dij优化 0ms)
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #defin ...