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. Delphi图像处理 -- RGB与HSL转换

    阅读提示:     <Delphi图像处理>系列以效率为侧重点,一般代码为PASCAL,核心代码采用BASM.     <C++图像处理>系列以代码清晰,可读性为主,全部使用C ...

  2. WebChat 清爽来袭 -- JavaChat系统Ⅳ

    题记:          WebChat 依然搭配的是 JavaChat 服务器,在不修改原来服务器代码的情况下进行使用终端的扩展.         此次项目的难点在于,Action/Servlet ...

  3. ASP.NET 防盗链的实现[HttpHandler]

    本文转载:http://www.cnblogs.com/eflylab/archive/2008/06/16/1223373.html 有时我们需要防止其他网站直接引用我们系统中的图片,或下载文件链接 ...

  4. [PWA] 11. Serve skeleton cache for root

    Intead of cache the root floder, we want to cache skeleton instead. self.addEventListener('install', ...

  5. iOS报错Expected selector for Objective-C method

    这个报错非常恶心:原因竟然是在导入头文件的地方多写了一个"+"号,可能问题在一个文件,报错在另一个文件

  6. 获取Html中所有img的src

    /// <summary> /// 获取所有Img中的Src /// </summary> /// <param name="htmlText"> ...

  7. Android LayoutInflater和findViewById 源码详解

    LayoutInflater大家很熟悉,简单点说就是布局文件XML解析器,setContentView函数也是调用了LayoutInflater 用法: View view = LayoutInfla ...

  8. 对DNSPOD添加域名解析的一些见解

    1.主机记录这步比较简单,输入“www”表示比较常规的域名例如www.abc.com,“@”表示abc.com,“ * ”表示泛解析,匹配所有*.abc.com的域名. 2.记录类型这步,一般常用A记 ...

  9. drop table xx purge

    drop table xx purge; 说明: 所有删除的表都会在回收站里面,只有后面加上purge才是彻底的清空表. (一般用于测试.练习数据表,所以最好不要带purge,要不误删就找不到了.)

  10. [转] UIImage 图像-IOS开发 (实例)

    转自  http://justcoding.iteye.com/blog/1470931 一 UIImageView 简介 UIImageView是显示图片的控件,显示图片时,首先需要把图片加载到UI ...