CH1808 Milking Grid
题意
POJ2185 数据加强版
描述
Every morning when they are milked, the Farmer John's cows form a rectangular grid that is R (1 <= R <= 10,000) rows by C (1 <= C <= 75) columns. As we all know, Farmer John is quite the expert on cow behavior, and is currently writing a book about feeding behavior in cows. He notices that if each cow is labeled with an uppercase letter indicating its breed, the two-dimensional pattern formed by his cows during milking sometimes seems to be made from smaller repeating rectangular patterns.
Help FJ find the rectangular unit of smallest area that can be repetitively tiled to make up the entire milking grid. Note that the dimensions of the small rectangular unit do not necessarily need to divide evenly the dimensions of the entire milking grid, as indicated in the sample input below.
输入
Line 1: Two space-separated integers: R and C
Lines 2..R+1: The grid that the cows form, with an uppercase letter denoting each cow's breed. Each of the R input lines has C characters with no space or other intervening character.
输出
- Line 1: The area of the smallest unit from which the grid is formed
样例输入
2 5
ABABA
ABABA
样例输出
2
提示
The entire milking grid can be constructed from repetitions of the pattern 'AB'.
来源
USACO 2003 Fall
分析
参照maxmercer的题解。
所谓正解?(伪)
- 求的所有行的循环节求max值,再求列的循环节的max值,两者相乘.
反例:
2 6
ABCDAB
ABCABC
用1求出来是8.实际上是12. - 将循环节求最小公倍数,若大于行(列)数就变为行(列)数.
反例:
2 8
ABCDEFAB
ABABAAAB
用2算出来因为行的循环节为5,6,最小公倍数30,因为大于8就转化为8,再乘上竖着的2,答案为16.
实际上答案应该是12,为:
ABCDEF
ABABAA
事实证明,POJ的数据太水了...网上很多题解都是错误的.
正解(真)
我们对与行和列进行hash操作.我们首先将所有列hash,那么每一列变成一个数,也就将矩阵压缩成了一行.再进行kmp的next预处理,求出循环节.再对行hash,列kmp,求出循环节,相乘即可.
正确性?
因为对于一串独立的字符串,他的hash值是唯一的.那么在行kmp的时候一列成了一个数值,那么两列数值相同当且仅当两列字符相同.因为最小矩阵覆盖不会出现错开的情况,是对齐的,那么一列可能穿过多次最小矩阵,那在一个最小矩阵长度之后的那一列,应该是与他相同的,因为我们是对齐了的.对行同理.
时间复杂度\(O(RC)\)
代码
#include<bits/stdc++.h>
typedef unsigned long long ULL;
const int maxn=10005,base=131;
int r,c,ans,len,nxt[maxn];
char s[maxn][105];
ULL temp[maxn],line[maxn],row[maxn];
int kmp_nxt(){
int i=0,j=-1;nxt[0]=-1;
while(i<len){
if(j==-1||temp[i]==temp[j]) ++i,++j,nxt[i]=j;
else j=nxt[j];
}
return len-nxt[len];
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
scanf("%d%d",&r,&c);
for(int i=0;i<r;++i) scanf("%s",s[i]);
for(int i=0;i<c;++i) for(int j=0;j<r;++j) line[i]=line[i]*base+s[j][i];
for(int i=0;i<c;++i) temp[i]=line[i];
len=c,ans=kmp_nxt();
for(int i=0;i<r;++i) for(int j=0;j<c;++j) row[i]=row[i]*base+s[i][j];
for(int i=0;i<r;++i) temp[i]=row[i];
len=r,ans*=kmp_nxt();
printf("%d\n",ans);
return 0;
}
CH1808 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 ...
- 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 ...
随机推荐
- SpringMVC—概述
mvc容器的实例化: http://blog.csdn.net/lin_shi_cheng/article/details/50686876 Spring的启动过程: 1: 对于一个web应用,其部署 ...
- Ubuntu下的MongoDB GUI 可视化管理工具
目录 1 Robo 3T 2 NoSQLBooster for MongoDB(收费) 3 JetBrains Plugin Repository :: Mongo Plugin Ubuntu下的Mo ...
- 20145221 《Java程序设计》第八周学习总结
20145221 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章部分 - 通用API 通用API 日志: 日志对信息安全意义重大,审计.取证.入侵检测等都会用到日志信息 日 ...
- Dijkstra算法补分
要求 Dijkstra算法,求解附图顶点A的单源最短路径 在纸上画出求解过程,上传截图(注意图上要有自己的学号和姓名) 过程
- ubuntu 18.04 64bit没有声音如何解决
一.背景 1.1 笔者的机器有两张声卡,使用aplay -l可以列举出来,一张是内置声卡,另一张是显卡自带的声卡,说明声卡驱动是ok的 1.2 笔者是在浏览器中播放视频无声音 二.尝试 2.1 尝试使 ...
- 【MVC - 参数原理】详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析]
前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/spring ...
- luogu P1162 填涂颜色
https://www.luogu.org/problem/show?pid=1162 //其实很简单的吧 //就是最外圈加一圈0 ,然后把外圈里面的0都遍历了 //剩下的0 就变成2 就行了 #in ...
- 【异常记录(六)】vs文件乱码:文件加载,使用Unicode(UTF-8)编码加载文件xxx时,有些字节已用Unicode替换字符替换。保存该文件将不会保留原始文件内容。
VS2013偶遇这种情况,页面汉字编码出现乱码. .... 按照网上查到的: 工具>选项>文本编辑器> 勾选了 然并卵,还是乱码... 其实炒鸡简单 用记事本打开另存为,选择 ...
- 前端工程化 - gulp
gulp是什么 gulp就是一个前端的自动化构建工具,在开发过程中很多重复的任务可以使用gulp和gulp插件自动完成.相比于grunt,gulp非常好上手,核心API只有4个,而且还有丰富的插件库. ...
- Java回顾之I/O
这篇文章主要回顾Java中和I/O操作相关的内容,I/O也是编程语言的一个基础特性,Java中的I/O分为两种类型,一种是顺序读取,一种是随机读取. 我们先来看顺序读取,有两种方式可以进行顺序读取,一 ...