先对每行求出所有可能的循环节长度(不需要整除)。

然后取在所有行中都出现了的,且最小的长度为宽。

然后将每一行看作字符,对所有行求next数组,将n-next[n](对这些行来说最小的循环节长度)作为长。

最后输出长乘宽即可。

#include<cstdio>
#include<cstring>
using namespace std;
bool can[10010][80];
char s[10010][80];
int next[80],n,m,wide,NEXT[10010];
void GetFail(char P[],int next[])//next[i]表示s[0]~s[i-1]的前缀中,最大相等的前后缀的长度是多少
{
next[0]=-1;
int len=strlen(P);
for(int i=0;i<len;i++)
{
int j=next[i];
while(j>=0 && P[i]!=P[j])
j=next[j];
if(j!=-1 && P[i]==P[j])
next[i+1]=j+1;
else next[i+1]=0;
}
}
int main()
{
// freopen("poj2185.in","r",stdin);
scanf("%d%d",&n,&m);
for(int i=0;i<n;++i)
{
scanf("%s",s[i]);
GetFail(s[i],next);
for(int j=next[m];j!=-1;j=next[j])
can[i][m-j]=1;
}
for(int j=1;j<=m;++j)
{
bool flag=1;
for(int i=0;i<n;++i)
if(!can[i][j])
{
flag=0;
break;
}
if(flag)
{
wide=j;
break;
}
}
for(int i=0;i<n;++i)
s[i][wide]='\0';
next[0]=-1;
int len=n;
for(int i=0;i<len;i++)
{
int j=next[i];
while(j>=0 && strcmp(s[i],s[j])!=0)
j=next[j];
if(j!=-1 && strcmp(s[i],s[j])==0)
next[i+1]=j+1;
else next[i+1]=0;
}
printf("%d\n",wide*(n-next[n]));
return 0;
}

【kmp算法】poj2185 Milking Grid的更多相关文章

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

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

  2. poj2185 Milking Grid【KMP】

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

  3. POJ2185 Milking Grid 【lcm】【KMP】

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

  4. POJ2185 Milking Grid 题解 KMP算法

    题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...

  5. POJ2185 Milking Grid KMP两次(二维KMP)较难

    http://poj.org/problem?id=2185   大概算是我学KMP简单题以来最废脑子的KMP题目了 , 当然细节并不是那么多 , 还是码起来很舒服的 , 题目中描写的平铺是那种瓷砖一 ...

  6. [USACO2003][poj2185]Milking Grid(kmp的next的应用)

    题目:http://poj.org/problem?id=2185 题意:就是要求一个字符矩阵的最小覆盖矩阵,可以在末尾不完全重合(即在末尾只要求最小覆盖矩阵的前缀覆盖剩余的尾部就行了) 分析: 先看 ...

  7. 字符串KMP || POJ 2185 Milking Grid

    求一个最小矩阵,经过复制能够覆盖原矩阵(覆盖,不是填充,复制之后可以有多的) *解法:横着竖着kmp,求最大公倍数的做法是不对的,见http://blog.sina.com.cn/s/blog_69c ...

  8. poj2185 Milking Grid

    题目链接:http://poj.org/problem?id=2185 这道题我看了好久,最后是通过参考kuangbin的博客才写出来的 感觉next数组的应用自己还是掌握的不够深入 这道题其实就是先 ...

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

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

随机推荐

  1. 解决Vue方法中setTimeout改变变量的值无效

    把data里的变量继承过来重新封装一下 let that = this; this.rightAnswer = false; setTimeout(function() { that.rightAns ...

  2. 简单瞎搞题(bitset的操作)

    链接:https://www.nowcoder.com/acm/contest/132/C来源:牛客网 题目 一共有 n个数,第 i 个数是 xi  xi 可以取 [li , ri] 中任意的一个值. ...

  3. TCP ------ TCP四次挥手(断开连接)及断开过程

    1.正常情况下,调用close(),产生的其中一个效果就是发送FIN,只有双方都调用close(),才会出现正常的四次挥手. 2.如果是服务器,发起四次挥手是在关闭accept()返回的套接字,而不是 ...

  4. socket编程 ------ BSD socket API

    伯克利套接字(Berkeley sockets),也称为BSD Socket.伯克利套接字的应用编程接口(API)是采用C语言的进程间通信的库,经常用在计算机网络间的通信. BSD Socket的应用 ...

  5. 480000 millis timeout while waiting for channel to be ready for write异常处理

    2014-08-25 15:35:05,691 ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: DatanodeRegistration( ...

  6. powershell for rename server name

    Rename server name if server has not yet joined AD $Hostname = "newname" $username = " ...

  7. 播放video

    <html> <head> <title> four in one vedio</title> <style type="text/cs ...

  8. EL表达式中获取list长度(JSTL函数用法)

    在jsp页面中不能通过${list.size}取列表长度,而是 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" pref ...

  9. 编辑器KindEditor的使用

    1.具体使用方法看点这里 2.下载点这里 3.文件夹说明 ├── asp asp示例,删掉 ├── asp.net asp.net示例,删掉 ├── attached 空文件夹,放置关联文件attac ...

  10. 关于Solaris的一些小技巧

    关于Solaris的一些小技巧 http://blog.chinaunix.net/uid-9787800-id-2394301.html SunOS 操作命令及linux区别 http://blog ...