Description

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.

Input

* 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.

Output

* Line 1: The area of the smallest unit from which the grid is formed 

Sample Input

2 5
ABABA
ABABA

Sample Output

2

Hint

The entire milking grid can be constructed from repetitions of the pattern 'AB'.

Source

【分析】
一开始还想错了,
其实是理解题意错了。
直接枚举每行的重复长度,然后找到一个使得每行都可以重复的长度。
竖行HASH+KMP
 /*
唐代李白
《登金陵凤凰台》 凤凰台上凤凰游,凤去台空江自流。
吴宫花草埋幽径,晋代衣冠成古丘。
三山半落青天外,二水中分白鹭洲。
总为浮云能蔽日,长安不见使人愁
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int MAXM = + ;
const int INF = ;
const int SIZE = ;
const int maxnode = 0x7fffffff + ;
using namespace std;
int l1, l2;
int next[MAXN];//不用开太大了..
int Ans[MAXN];
char data[MAXN][MAXM];
int r, c;//长和宽 /*void getNext(){
next[1] = 0;
int j = 0;
for (int i = 2; i <= n; i++){
while (next[j] > 0 && strcmp(data[j + 1] + 1, data[i] + 1)) j = next[j];
if (!strcmp(data[j + 1] + 1, data[i] + 1)) j++;
next[i] = j;
}
return;
}*/
//a是模板链, b是匹配串
/*int kmp(char *a, char *b){
int j = 0, cnt = 0;
for (int i = 1; i <= l2; i++){
while (next[j] > 0 && a[j + 1] == b[i]) j = next[j];
if (a[j + 1] == b[i]) j++;
if (j == m) return 1;//?
}
}*/ /*void init(){
scanf("%s", a + 1);
scanf("%s", b + 1);
l1 = strlen(a + 1);
l2 = strlen(b + 1);
}*/
int cnt[MAXM], h[MAXN];
char a[MAXM]; void init(){
scanf("%d%d", &r, &c);
memset(cnt, , sizeof(cnt));
for (int i = ; i < r; i++){
scanf("%s", data[i]);
strcpy(a, data[i]);
for(int j = c - ; j > ; j--){
a[j]=;
int x = , y;
for(y = ; data[i][y] ; y++){
if(!a[x]) x = ;
if(a[x] != data[i][y]) break;
x++;
}
if( !data[i][y] ) cnt[j]++;
}
}
}
void work(){
int i;
for(i = ; i < c; i++) if(cnt[i] == r)break;
int x = i;
for(int i = ;i < r; i++) data[i][x] = ;
next[] = -;//按纵列求KMP的next函数,以求最小重复子矩阵的行数
for(int i = , j = -; i < r; i++){
while(j != - && strcmp(data[j+],data[i])) j = next[j];
if(!strcmp(data[j+], data[i])) j++;
next[i] = j;
}
printf("%d\n",(r - - next[r-]) * x);//行列相乘即为最终结果
} int main(){
int T; init();
work(); return ;
}
 

【POJ2185】【KMP + HASH】Milking Grid的更多相关文章

  1. 7.26机房报零赛——无尽的矩阵【kmp+hash】

    恩,其实大家都没有报零,反正我是蒟蒻 为了纪念我第一次打过哈希,特此写一篇题解 题目描述 从前有一个的小矩阵,矩阵的每个元素是一个字母(区分大小写),突然有一天它发生了 变异,覆盖了整个二维空间,即不 ...

  2. bzoj 4825: [Hnoi2017]单旋【dfs序+线段树+hash】

    这个代码已经不是写丑那么简单了--脑子浆糊感觉np++分分钟想暴起打死我--就这还一遍A过了-- 先都读进来hash一下,因为是平衡树所以dfs序直接按照点值来就好 对于每个操作: 1:set维护已插 ...

  3. bzoj 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛【dp+树状数组+hash】

    最长上升子序列.虽然数据可以直接n方但是另写了个nlogn的 转移:f[i]=max(f[j]+1)(a[j]<a[i]) O(n^2) #include<iostream> #in ...

  4. 【CF1257F】Make Them Similar【meet in the middle+hash】

    题意:给定n个数,让你给出一个数,使得n个数与给出的数异或后得到的数的二进制表示中1的数量相同 题解:考虑暴搜2^30去找答案,显然不可接受 显然可以发现,这是一个经典的meet in the mid ...

  5. P3706-[SDOI2017]硬币游戏【高斯消元,字符串hash】

    正题 题目链接:https://www.luogu.com.cn/problem/P3706 题目大意 给出 \(n\) 个长度为 \(m\) 的 \(H/T\) 串. 开始一个空序列,每次随机在后面 ...

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

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

  7. poj2185 Milking Grid【KMP】

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

  8. POJ2185 Milking Grid 【lcm】【KMP】

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

  9. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

随机推荐

  1. 【转】Ubuntu14.04搭建安装svnserver

    原文网址:http://www.cnblogs.com/blfshiye/p/5168028.html 前两天,公司准备搭建一个svnserver,供大家使用.于是.就先装了一个Ubuntu系统,然后 ...

  2. UIImageVIew的使用

    UIImageView是一个用于显示图片的控件 构造方法:     UIImage * tempImage = [UIImage imageNamed:IMAGE_NAME];     imageVi ...

  3. 钥匙计数之一 - HDU 1438(状态压缩打表)

    分析:首先想到每个钥匙的结尾有4种状态,不过题目还需要判断有三种不同的钥匙深度,所以每种深度结尾后有2^4种状态,0000->1111,不过题目还需需要有相邻的钥匙深度大于等于3,所以需要两种不 ...

  4. js冲突怎么解决

    a.最容易出现的就是js的命名冲突①.变量名冲突变量有全局变量和局部变量当全局变量变量和局部变量名称一致时,就会js冲突,由于变量传递数值或地址不同就会产生JavaScript错误,甚至死循环.②.方 ...

  5. 关于fork有意思的两道题目

    http://www.spongeliu.com/123.html 第一题,计算下面代码理论上总共打印了多少行:(网易2011笔试题) #include #include #include int m ...

  6. maven常用插件配置

    1.maven-jar-plugin插件 <!-- 排除资源文件中的properties文件,不需要打到jar中,后面通过assembly插件打包到conf目录中 --><plugi ...

  7. c#基础语言编程-常用函数

    类型转换Convert Convert考虑数据意义的转换. Convert是一个加工.改造的过程.在使用Convert的转换过程中不会返回异常,当遇到类型转换的不知道的时候,用Convert找找. T ...

  8. FZU 2104 (13.11.28)

    Problem 2104 Floor problem Accept: 376    Submit: 433 Time Limit: 1000 mSec    Memory Limit : 32768 ...

  9. 封装Unity3d的dll时的经验总结

    部分时候,我们需要自己封装一些小工具来简化我们的工作. 实验时,偶然发现Unity3d的console在双击进行debug信息的输出定位时,只能跟进到dll的上一层,因此我们可以将unity3d自带的 ...

  10. 小程序原理,生成SQL SERVER 2008 数据库所有表的结构文档

    作者:wide288 , 日期:2013-7-31 以前开发中,用 MYSQL 数据库,有个小程序 生成数据库结构文档.很方便,做为开发组的文档很有用. 现在开发中用到了 SQL SERVER 200 ...