【POJ2185】【KMP + HASH】Milking Grid
Description
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
* 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
Sample Input
2 5
ABABA
ABABA
Sample Output
2
Hint
Source
/*
唐代李白
《登金陵凤凰台》 凤凰台上凤凰游,凤去台空江自流。
吴宫花草埋幽径,晋代衣冠成古丘。
三山半落青天外,二水中分白鹭洲。
总为浮云能蔽日,长安不见使人愁
*/
#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的更多相关文章
- 7.26机房报零赛——无尽的矩阵【kmp+hash】
恩,其实大家都没有报零,反正我是蒟蒻 为了纪念我第一次打过哈希,特此写一篇题解 题目描述 从前有一个的小矩阵,矩阵的每个元素是一个字母(区分大小写),突然有一天它发生了 变异,覆盖了整个二维空间,即不 ...
- bzoj 4825: [Hnoi2017]单旋【dfs序+线段树+hash】
这个代码已经不是写丑那么简单了--脑子浆糊感觉np++分分钟想暴起打死我--就这还一遍A过了-- 先都读进来hash一下,因为是平衡树所以dfs序直接按照点值来就好 对于每个操作: 1:set维护已插 ...
- 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 ...
- 【CF1257F】Make Them Similar【meet in the middle+hash】
题意:给定n个数,让你给出一个数,使得n个数与给出的数异或后得到的数的二进制表示中1的数量相同 题解:考虑暴搜2^30去找答案,显然不可接受 显然可以发现,这是一个经典的meet in the mid ...
- P3706-[SDOI2017]硬币游戏【高斯消元,字符串hash】
正题 题目链接:https://www.luogu.com.cn/problem/P3706 题目大意 给出 \(n\) 个长度为 \(m\) 的 \(H/T\) 串. 开始一个空序列,每次随机在后面 ...
- 字符串KMP || POJ 2185 Milking Grid
求一个最小矩阵,经过复制能够覆盖原矩阵(覆盖,不是填充,复制之后可以有多的) *解法:横着竖着kmp,求最大公倍数的做法是不对的,见http://blog.sina.com.cn/s/blog_69c ...
- 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 ...
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
随机推荐
- (转载)MySQL LIKE 用法:搜索匹配字段中的指定内容
(转载)http://www.5idev.com/p-php_mysql_like.shtml MySQL LIKE 语法 LIKE 运算符用于 WHERE 表达式中,以搜索匹配字段中的指定内容,语法 ...
- Web Services的相关名词解释:WSDL与SOAP
在对Web Services进行性能测试时,接触到最多的两个名词就是WSDL和SOAP.利用LoadRunner对Web Services进行调用的时候,也存在两种常用方法,即基于WSDL的[Add ...
- C/S结构与B/S结构的特点分析
C/S结构与B/S结构的特点分析 为了区别于传统的C/S模式,才特意将其称为B/S模式.认识到这些结构的特征,对于系统的选型而言是很关键的. 1.系统的性能 在系统的性能方面,B/S占有优势的是其异地 ...
- windows下protobuf jar包的编译
0.如果你不想手动编译生成,请直接跳到最后下载附件. 1.下载protobuf release版本:https://github.com/google/protobuf/releases,protoc ...
- [置顶] MyEclipse下安装插件方法(properties文件编辑器Propedit为例)
网上流传了很多安装插件的方法.在这里我只讲解一种方法. 因为我认为这种方法有以下两个优点:第一.简单,方便安装:第二.对于自己安装的插件易于管理. 我的myeclipse版本号为10.5,操作系统为w ...
- 使用Spring-data-redis操作Redis的Sentinel
介绍 Spring-Data-Redis项目(简称SDR) 是对Redis的Key-Value数据存储操作提供了更高层次的抽象,提供了一个对几种主要的redis的Java客户端(例 如:jedis,j ...
- memcached与redis 对比
一. 综述 读一个软件的源码,首先要弄懂软件是用作干什么的,那memcached和redis是干啥的?众所周知,数据一般会放在数据库中,但是查询数据会相对比较慢,特别是用户很多时,频繁的查询,需要耗费 ...
- UVaLive6039 Uva1668 Let's Go Green
一开始考虑所有边都是单独的一条路径 然后尽量多的合并 #include<cstdio> #include<cstring> #include<cstdlib> #i ...
- Epoll之ET、LT模式
Epoll之ET.LT模式 在使用epoll时,在函数 epoll_ctl中如果不设定,epoll_event 的event默认为LT(水平触发)模式. 使用LT模式意味着只要fd处于可读或者可写状态 ...
- 下载的chm手册打不开的解决方法?
用ie或者chrome等浏览器下载文件的时候,都会给文件加上一个默认的保护,右键这个文件,打开属性对话框,然后在第一个的选项卡的安全的部分,有个解除这个保护的按钮点下然后确定保存,再打开chm文件就不 ...