【KMP】POJ 2185 Milking Grid -- Next函数的应用
题目链接:http://poj.org/problem?id=2185
题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵。
题目分析:next函数的应用。需要枚举每一行每一列的字符串所对应的的 nxt[]
值,然后通过分析计算出最小的宽和最小的高。
具体分析
参考链接:https://blog.csdn.net/u013686535/article/details/52197467
一看这题,容易想出一种很直观的做法:求出每一行的最小重复串长度,取所有行的最小重复串长度的lcm为宽;对列也同样操作求出高。这种想法虽然很直观,但是否正确呢?
事实上,这种算法并不是正确的。如下面的这个反例:
2 8
ABCDEFAB
AAAABAAA
对于这个例子:第一行为6,第二行为5,6与5的最小公倍数为30,大于8则取8为宽,但明显是错误的。
但由于poj的测试数据太弱,以致使用这种方法的程序也可以通过。
下面介绍一下正解的做法。
首先是确定宽度:我们分别求出每行所有可能的重复子串长度,例如对于aaaa就有1、2、3和4,然后取每行都有的重复子串长度中最小的作为宽。
例如,对于上面的例子,第一行的重复子串长度只可能是6或8(显然整个串为一个重复子串也是可以的),第二行则可能是5、6、7或8,那么取它们都有的6和8当中最小值6,就是最小覆盖子矩阵的宽。
然后分成两步:
- 考虑到每行的列数比较小,
1<=C<=75
,可以直接暴力枚举重复子串的长度,每列逐一检查是否与当前枚举的重复子串相对应。因此求宽度这个部分的时间复杂度为O(R*C^2)
。 - 找到C以后我们再来一个个枚举R就可以了,时间复杂度也是
O(R*C^2)
。
不过我这里采用标记的方式,时间复杂度降到 O(R*C)
。
代码:
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 404000;
int m, nxt[maxn];
string t;
int N, M;
char ch[10010][80];
int cnt_c[80], cnt_r[10010];
void cal_next() {
m = t.length();
for (int i = 0, j = -1; i < m; i ++) {
while (j != -1 && t[j+1] != t[i]) j = nxt[j];
nxt[i] = (j+1 < i && t[j+1] == t[i]) ? ++j : -1;
}
}
void solve() {
// 首先清空用于记录的cnt_c和cnt_r数组
memset(cnt_c, 0 ,sizeof(cnt_c));
memset(cnt_r, 0, sizeof(cnt_r));
// 遍历每一行
for (int i = 0; i < N; i ++) {
t = "";
for (int j = 0; j < M; j ++) t += ch[i][j];
cal_next();
int j = m-1;
while (nxt[j] != -1) {
j = nxt[j];
cnt_c[m-1-j] ++;
}
}
// 遍历每一列
for (int i = 0; i < M; i ++) {
t = "";
for (int j = 0; j < N; j ++) t += ch[j][i];
cal_next();
int j = m-1;
while (nxt[j] != -1) {
j = nxt[j];
cnt_r[m-1-j] ++;
}
}
// 找到最小的cnt_c[i]==N,没有则M
int cc = 1, rr = 1;
for (; cc < M; cc ++) if (cnt_c[cc] == N) break;
// 找到最小的cnt_r[i]==M,没有则N
for (; rr < N; rr ++) if (cnt_r[rr] == M) break;
cout << rr * cc << endl;
}
int main() {
while (cin >> N >> M) {
for (int i = 0; i < N; i ++) cin >> ch[i];
solve();
}
return 0;
}
【KMP】POJ 2185 Milking Grid -- Next函数的应用的更多相关文章
- 字符串KMP || POJ 2185 Milking Grid
求一个最小矩阵,经过复制能够覆盖原矩阵(覆盖,不是填充,复制之后可以有多的) *解法:横着竖着kmp,求最大公倍数的做法是不对的,见http://blog.sina.com.cn/s/blog_69c ...
- POJ 2185 Milking Grid KMP循环节周期
题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...
- [poj 2185] Milking Grid 解题报告(KMP+最小循环节)
题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...
- POJ 2185 Milking Grid(KMP)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4738 Accepted: 1978 Desc ...
- POJ 2185 Milking Grid [二维KMP next数组]
传送门 直接转田神的了: Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6665 Accept ...
- POJ 2185 Milking Grid KMP(矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Lim ...
- POJ 2185 Milking Grid [KMP]
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8226 Accepted: 3549 Desc ...
- 题解报告:poj 2185 Milking Grid(二维kmp)
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- poj 2185 Milking Grid
Milking Grid http://poj.org/problem?id=2185 Time Limit: 3000MS Memory Limit: 65536K Descript ...
随机推荐
- redis详解(包含使用场景)
本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么这么快4.redis的数据类型,以及每种数据类型的使用场景5.redis的过期策略以及内存淘汰 ...
- C# 函数参数中的this
先看下面的代码: public static class StringExtension { public static void Foo(this string s) { Console.Write ...
- YAML_14 tags给指定的任务定义一个调用标识,以后不用重复整个过程,只需要执行tags标签的部分
ansible]# vim adhttp.yml --- - hosts: cache remote_user: root tasks: - copy: src: /r ...
- PHP获取POST数据的三种方式
一.PHP获取POST数据的几种方法 方法1.最常见的方法是:$_POST['fieldname']; 说明:只能接收Content-Type: application/x-www-form-urle ...
- loj 3102
题目大意: 给定 \(m\) 棵无向树\(\left\{T_{1}=\left(V_{1}, E_{1}\right), T_{2}=\left(V_{2}, E_{2}\right), \cdots ...
- CODE FESTIVAL 2017 qual C 题解
传送门 \(A\) 咕咕 const int N=15; char s[N];int n; int main(){ scanf("%s",s+1),n=strlen(s+1); f ...
- OpenFOAM显示残差
本文主要讲解两种方法用来显示OpenFOAM的计算残差,一种是采用OpenFOAM自带的foamMonitor来输出残差,另一种就是大家经常看见的采用pyFoam来输出残差.不管采用哪一种方法都必须安 ...
- UDF-C_UDMI【转载】
UDF定义变量的输出 使用宏: C_UDMI( c, thread, index) 自变量类型:cell_t c Thread *thread int index ...
- Jmeter聚合报告理解
 Label:每个 JMeter 的 element(例如 HTTP Request)都有一个 Name 属性,这里显示的就是 Name 属性的值 Samples:表示这次测试中一共发出了多少个请求 ...
- SpringBoot集成mybatis,同时读取一个数据库中多个数据表
SpringBoot集成mybatis,同时读取一个数据库中多个数据表: application.properties: mybatis.config-location=classpath:mybat ...