【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),每个 ...
随机推荐
- Spark SQL inferSchema实现原理探微(Python)
使用Spark SQL的基础是“注册”(Register)若干表,表的一个重要组成部分就是模式,Spark SQL提供两种选项供用户选择: (1)applySchema applySche ...
- HTML---网页编程(1)
前 言 HTML需要和CSS还有JS一起用,才能提现强大. 所以,学了HTML.最好去学学CSS还有JS(JavaScript) ☆静态页面和动态页面 网站页面分为静态页面和动态页面两种 • 静态页面 ...
- bzoj 2819 Nim(BIT,dfs序,LCA)
2819: Nim Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1596 Solved: 597[Submit][Status][Discuss] ...
- CentOS 7 更改网卡名到以前的eth0
最近安装了CentOS7,内核总算升级到3.10,支持Linux容器,network namespace······· 但是安装完之后,发现ifconfig没看到熟悉的eth0,却是enp0s3,虽然 ...
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- thinkphp xml编码函数
/** * XML编码 * @param mixed $data 数据 * @param string $root 根节点名 * @param string $item 数字索引的子节点名 * @pa ...
- Linux 操作系统位数(32or64)识别
Linux 操作系统位数识别: LINUX 32位操作系统:Linux x86 i586 i386 i686 i... LINUX 64位操作系统:Linux x64x86_64 X64 . ...
- Python基础知识--列表和集合
列表:有序性,可以存放任意类型的对象,通过索引访问,可以分片操作 >>> L = ['id', 1000, 'scd', 1000, 'scd'] >>> L [' ...
- SDL 实现透明悬浮窗
最近一直想用SDL实现弹幕功能,但是一直没法实现悬浮窗和透明背景功能. 在一个老外的博客上发现了思路:EthioProgrammer: Applying transparency using win3 ...
- 编写android的widget
以前对这个东西很感兴趣,因为确实方便,如今有时间了来做一个例子 首先要定义一个layout(widgetview.xml)和一个配置文件(widgetconfig.xml) <?xml vers ...