【kmp算法】poj2185 Milking Grid
先对每行求出所有可能的循环节长度(不需要整除)。
然后取在所有行中都出现了的,且最小的长度为宽。
然后将每一行看作字符,对所有行求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的更多相关文章
- 【POJ2185】【KMP + HASH】Milking Grid
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- poj2185 Milking Grid【KMP】
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10084 Accepted: 4371 Des ...
- POJ2185 Milking Grid 【lcm】【KMP】
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- POJ2185 Milking Grid 题解 KMP算法
题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...
- POJ2185 Milking Grid KMP两次(二维KMP)较难
http://poj.org/problem?id=2185 大概算是我学KMP简单题以来最废脑子的KMP题目了 , 当然细节并不是那么多 , 还是码起来很舒服的 , 题目中描写的平铺是那种瓷砖一 ...
- [USACO2003][poj2185]Milking Grid(kmp的next的应用)
题目:http://poj.org/problem?id=2185 题意:就是要求一个字符矩阵的最小覆盖矩阵,可以在末尾不完全重合(即在末尾只要求最小覆盖矩阵的前缀覆盖剩余的尾部就行了) 分析: 先看 ...
- 字符串KMP || POJ 2185 Milking Grid
求一个最小矩阵,经过复制能够覆盖原矩阵(覆盖,不是填充,复制之后可以有多的) *解法:横着竖着kmp,求最大公倍数的做法是不对的,见http://blog.sina.com.cn/s/blog_69c ...
- poj2185 Milking Grid
题目链接:http://poj.org/problem?id=2185 这道题我看了好久,最后是通过参考kuangbin的博客才写出来的 感觉next数组的应用自己还是掌握的不够深入 这道题其实就是先 ...
- POJ 2185 Milking Grid KMP(矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Lim ...
随机推荐
- 关于final局部变量引用的研究
嵌套类(内部类)方法安全引用外部方法局部变量的原理 嵌套类方法引用外部局部变量,必需将声明为final,否则将出现 Cannot refer to a non-final variable * ins ...
- Codeforces Round #350 (Div. 2) D1
D1. Magic Powder - 1 time limit per test 1 second memory limit per test 256 megabytes input standard ...
- The 13th Zhejiang Provincial Collegiate Programming Contest - C
Defuse the Bomb Time Limit: 2 Seconds Memory Limit: 65536 KB The bomb is about to explode! Plea ...
- mybatis的注解功能
一.mybatis 简单注解 关键注解词 : @Insert : 插入sql , 和xml insert sql语法完全一样 @Select : 查询sql, 和xml select sql语法完全一 ...
- photo@PKU
- Create a conditional DNS forwarder on our domain.com to Amazon default DNS provider
Backgroup: I have an AWS Managed Active Directory(domain.com). I created a DHCP options set to my d ...
- Extend the size of ext3 partition online in VM
1. Increase disk space in vCenter 2. scan disk on the Linux VM # fdisk -lu > /tmp/fdisk. # > / ...
- wait , notify 模拟 Queue
package com.itdoc.multi.sync009; import java.util.LinkedList; import java.util.concurrent.TimeUnit; ...
- 【poj3260-最少找零】多重背包+完全背包
多重背包+完全背包. 买家:多重背包:售货员:完全背包: 开两个数组,分别计算出买家,售货员每个面额的最少张数. 最重要的是上界的处理:上界为maxw*maxw+m(maxw最大面额的纸币). (网上 ...
- bzoj1578 [Usaco2009 Feb]Stock Market 股票市场
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1578 [题解] 由于连续买相当于每天买,第二天卖,然后再买.所以每天最后钱尽量多一定是最优的 ...