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的更多相关文章

  1. POJ 2185 Milking Grid KMP(矩阵循环节)

                                                            Milking Grid Time Limit: 3000MS   Memory Lim ...

  2. POJ 2185 Milking Grid(KMP)

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4738   Accepted: 1978 Desc ...

  3. 【POJ2185】【KMP + HASH】Milking Grid

    Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...

  4. POJ 2185 Milking Grid [KMP]

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8226   Accepted: 3549 Desc ...

  5. poj 2185 Milking Grid

    Milking Grid http://poj.org/problem?id=2185 Time Limit: 3000MS   Memory Limit: 65536K       Descript ...

  6. poj2185 Milking Grid【KMP】

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10084   Accepted: 4371 Des ...

  7. CH1808 Milking Grid

    题意 POJ2185 数据加强版 描述 Every morning when they are milked, the Farmer John's cows form a rectangular gr ...

  8. POJ2185 Milking Grid 【lcm】【KMP】

    Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...

  9. AC日记——Milking Grid poj 2185

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8314   Accepted: 3586 Desc ...

  10. POJ 2185 Milking Grid [二维KMP next数组]

    传送门 直接转田神的了: Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6665   Accept ...

随机推荐

  1. windows7实用快捷键 分类: windows常用小技巧 2014-04-18 14:34 169人阅读 评论(0) 收藏

    几个比较实用的快捷键 windows键:简写成win win+   方向键上下,可以使当前窗体放大或缩小 win+   方向键左右,可以使当前窗体悬靠在左边或右边 win+Home   仅保留当前窗体 ...

  2. js~this的陷阱

    在JS中,当前对象一般用this表示,在jquery中,当前的对象是用$(this)表示,这些都是最基础的知识,没什么可说的,但我要说的是,当this出现在某个深度时,它的含义你自己要清楚,它是指离当 ...

  3. android scrollview组件禁止滑动的方法

    xml配置:           android:id="@+id/sc_freement"             android:layout_width="fill ...

  4. Android FastJson解析

    1.导包 compile 'com.alibaba:fastjson:1.2.21' public static final Object parse(String text); // 把JSON文本 ...

  5. Java基础知识强化之IO流笔记05:try...catch...finally包含的代码是运行期的

    1. 代码示例: 上面看到的第13行:  Date  d = null;(这里必须初始化) 第14~20行使用try...catch...finally包含代码,这里的代码已经变成运行期代码.此时我们 ...

  6. 向量旋转 UPC 2217

    这道题目是13山东省省赛的签到题,题目大意是给等边三角形的两个定点,让求逆时针旋转之后的第三个点的坐标,原来不会向量的旋转,在网上找了找,找到一篇挺好的,直接贴过来. 向量的旋转 实际做题中我们可能会 ...

  7. Python开发【第八篇】:网络编程

    Python之路[第六篇]:socket   Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字&quo ...

  8. 17个提升iOS开发效率的必用工具

    时间就是金钱.编码效率的提升意味着更多的收入.可是当我们的开发技巧已经到达一定高度时,如何让开发效率更上一层楼呢?答案就是使用开发工具!在这篇文章中,我会向你介绍一些帮助我们提升编码速度和工作效率的工 ...

  9. ASP.NET菜鸟之路之Request小例子

    背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. Request获取值 Request获取值有两种 ...

  10. Maven Profile标签

    Maven Profiles标签可以针对不同的环境来使用不同的配置文件 在发布的时候可以用 mvn release -p product mvn release -p test mvn release ...