题意

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的题解。

所谓正解?(伪)

  1. 求的所有行的循环节求max值,再求列的循环节的max值,两者相乘.

    反例: 

    2 6

    ABCDAB

    ABCABC 

    用1求出来是8.实际上是12.
  2. 将循环节求最小公倍数,若大于行(列)数就变为行(列)数.

    反例:

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

  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. POJ2185 Milking Grid 【lcm】【KMP】

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

  8. AC日记——Milking Grid poj 2185

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

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

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

随机推荐

  1. Java 和 Python 解析动态 key 的 JSON 数据

    一.概述 解析JSON过程中,什么情况都可能遇到.遇到特殊的情况,不会怎么办?肯定不是设计的问题,一定是你的姿势不对. 有这样一种JSON需要解析: { "b3444533f6544&quo ...

  2. VS2010/MFC编程入门之前言

    鸡啄米的C++编程入门系列给大家讲了C++的编程入门知识,大家对C++语言在语法和设计思想上应该有了一定的了解了.但是教程中讲的例子只是一个个简单的例程,并没有可视化窗口.鸡啄米在这套VS2010/M ...

  3. animation CSS3动画总结

    最近一个小游戏项目用到了CSS3的动画属性,例如transition.transform.animation.经过三个星期,终于做完了,利用周末好好梳理总结一下. keyframes这个属性用来定义一 ...

  4. myeclips破解

    MyEclipse官方安装文件,下载地址 http://www.jb51.net/softs/150886.html破解补丁http://www.jb51.net/softs/150887.html ...

  5. linux内核分析第四周-使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用

    本周作业的主要内容就是采用gcc嵌入汇编的方式调用system call.系统调用其实就是操作系统提供的服务.我们平时编写的程序,如果仅仅是数值计算,那么所有的过程都是在用户态完成的,但是我们想将变量 ...

  6. FastCGI介绍及Nginx fastcgi配置优化

    FastCGI介绍 FastCGI是从CGI发展改进而来的.传统CGI接口方式的主要缺点是性能很差,因为每次HTTP服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后结果被返回给HTTP服务 ...

  7. Spring Boot与数据

    SpringBoot 着眼于JavaEE! 不仅仅局限于 Mybatis .JDBC. Spring Data JPA Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数 ...

  8. shell 判断字符串长度是否为0

    test.sh #!/bin/bash echo "enter the string:" read filename if test -z $filename ; then ech ...

  9. python 进制转换

    print hex(),hex(-) #转换成十六进制 print oct(),oct(-) #转换成八进制 print bin(),bin(-) #转换成二进制 print int("字面 ...

  10. 不能用Xming连接到 Centos 7

    修改/etc/gdm/custom.conf 之后, Centos 6是没有问题的. 可是Centos 7 不行. 根据这个连接,运行firewall-config,把网络改到trusted级别, 能 ...