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. 改进的sqlhelper学习日志

    下面就是详细的sqlhelper的代码了 using System; using System.Collections.Generic; using System.Linq; using System ...

  2. HDU-4974 A simple water problem

    http://acm.hdu.edu.cn/showproblem.php?pid=4974 话说是签到题,我也不懂什么是签到题. A simple water problem Time Limit: ...

  3. 游戏开发设计模式之命令模式(unity3d 示例实现)

    博主才学尚浅,难免会有错误,尤其是设计模式这种极富禅意且需要大量经验的东西,如果哪里书写错误或有遗漏,还请各位前辈指正. 打 算写设计模式的目的就是,首先自己可以理清思路,还有就是国内的设计模式资料很 ...

  4. 曾经的岁月之maya

  5. 关于OC头文件互相引用的问题

    在OC中头文件互相引用是很常见的一件事,如: A的头文件#import "B.h" 而B的头文件#import "A.h" 这个时候就会出现意想不到的问题.系统 ...

  6. C++Primer第5版学习笔记(一)

    C++Primer第5版学习笔记(一) 第一.二章的重难点内容        这个笔记本主要记录了我在学习C++Primer(第5版,中文版)的过程中遇到的重难点及其分析.因为第一.二章都比较简单,因 ...

  7. Action

    学习Action的几个内容 1.实现一个Action的最常用方式: 从ActionSupport继承     链接 2.Action配置 DMI动态方法调用 ! 通配符配置 * {1} {2} … * ...

  8. 一个表的两个列连接另外一个表的一个列SQL语句怎么写

    f619424517 | 浏览 2207 次 推荐于2016-09-09 11:38:18   最佳答案   select a.flightid,a.flightname,b.cityname,c.c ...

  9. 用Excel完成专业化数据统计、分析工作

    使用Excel可以完成很多专业软件才能完成的数据统计.分析工作,比如:直方图.相关系数.协方差.各种概率分布.抽样与动态模拟.总体均值判断,均值推断.线性.非线性回归.多元回归分析.时间序列等.本专题 ...

  10. 语音控制的tab选项卡

    前端开发whqet,csdn,王海庆,whqet,前端开发专家 ladies and 乡亲们,程序猿同志们,周末仍然坚守工作岗位,或者学习不辍的童鞋们,福音来了. 语音识别高不高端.难不难? 今天给大 ...