POJ 2185 kmp
题目链接:http://poj.org/problem?id=2185
题意:给出一个R*C(10000 * 75)的矩形字符串,然你求最小的不严格重复矩阵,比如,ABA,最小的重复矩阵是AB,经过复制ABAB,可以把ABA完全覆盖掉。
题解:
题意很好理解,首先我们应该清楚,最小重复字串一定实在左上角。我们可以找到宽的最小共同重复串,然后,我们就可以对高进行kmp。答案即为w*h的值。对于宽,我们可以暴力找,枚举每一行的所有起点为0的字串,判断该长度是否满足重复,然后,我们所有行的最小共同的重复字串,即为宽w,对于高,只需要进行一次kmp求最小重复字串即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e4 + ;
char s[maxn][], T[];
int net[maxn];
int bit[];
int R, C;
int main()
{
int ans = ;
scanf("%d%d", &R, &C);
for(int i = ; i < C; i++) bit[i] = ;
for(int i = ; i < R; i++)
{
scanf("%s", s[i]);
strcpy(T, s[i]);
for(int j = C - ; j >= ; j--)
{
T[j] = ;
int x = , y = ;
for(; s[i][y]; x++, y++)
{
if(!T[x]) x = ;
if(T[x] != s[i][y]) break;
}
if(!s[i][y]) bit[j]++;
}
}
for(int i = ; i < C; i++)
if(bit[i] == R)
{
ans = i;
break;
}
if(!ans) ans = C;
for(int i = ; i < R; i++)
s[i][ans] = ;
net[] = -;
int j = , k = -;
while(j < R)
{
if(k == - || !strcmp(s[k], s[j])) net[++j] = ++k;
else
k = net[k];
}
printf("%d\n", (R - net[R])*ans); //行列相乘即为最终结果
return ;
}
有一个优化:http://poj.org/showmessage?message_id=168710
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e4 + ;
char s[maxn][], T[];
int net[maxn];
int bit[];
int R, C;
int main()
{
int ans = ;
scanf("%d%d", &R, &C);
for(int i = ; i < C; i++) bit[i] = ;
for(int i = ; i < R; i++)
{
scanf("%s", s[i]);
strcpy(T, s[i]);
for(int j = C - ; j >= ; j--)
{
T[j] = ;
int x = , y = ;
for(; s[i][y]; x++, y++)
{
if(!T[x]) x = ;
if(T[x] != s[i][y]) break;
}
if(!s[i][y]) bit[j]++;
}
}
for(int i = ; i < C; i++)
if(bit[i] == R)
{
ans = i;
break;
}
if(!ans) ans = C;
net[] = -;
int j = , k = -;
while(j < R)
{
if(k == - || !strcmp(s[k], s[j])) net[++j] = ++k;
else
k = net[k];
}
printf("%d\n", (R - net[R])*ans); //行列相乘即为最终结果
return ;
}
POJ 2185 kmp的更多相关文章
- POJ 2185 Milking Grid KMP循环节周期
题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...
- Milking Grid POJ - 2185 || 最小覆盖子串
Milking Grid POJ - 2185 最小覆盖子串: 最小覆盖子串(串尾多一小段时,用前缀覆盖)长度为n-next[n](n-pre[n]),n为串长. 当n%(n-next[n])==0时 ...
- [poj 2185] Milking Grid 解题报告(KMP+最小循环节)
题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...
- 二维KMP - 求字符矩阵的最小覆盖矩阵 - poj 2185
Milking Grid Problem's Link:http://poj.org/problem?id=2185 Mean: 给你一个n*m的字符矩阵,让你求这个字符矩阵的最小覆盖矩阵,输出这个最 ...
- 【poj 2185】Milking Grid(字符串--KMP+问题分解)
题意:给定一个由字符组成的矩阵,求出它的面积最小的覆盖矩阵.(感觉应该是可重叠的......* (・ω・)っ) 解法:KMP.行列互不影响,可以问题分解.先求出每一行的最小重复串,利用kmp中的nex ...
- POJ 2185 Milking Grid (KMP,求最小覆盖子矩阵,好题)
题意:给出一个大矩阵,求最小覆盖矩阵,大矩阵可由这个小矩阵拼成.(就如同拼磁砖,允许最后有残缺) 正确解法的参考链接:http://poj.org/showmessage?message_id=153 ...
- POJ 2185 Milking Grid(KMP)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4738 Accepted: 1978 Desc ...
- POJ 2185 Milking Grid(KMP最小循环节)
http://poj.org/problem?id=2185 题意: 给出一个r行c列的字符矩阵,求最小的覆盖矩阵可以将原矩阵覆盖,覆盖矩阵不必全用完. 思路: 我对于字符串的最小循环节是这么理解的: ...
- POJ 2185 Milking Grid [二维KMP next数组]
传送门 直接转田神的了: Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6665 Accept ...
随机推荐
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
在上一篇Spring中使用JdbcTemplate访问数据库中介绍了一种基本的数据访问方式,结合构建RESTful API和使用Thymeleaf模板引擎渲染Web视图的内容就已经可以完成App服务端 ...
- 【洛谷 P4134】 [BJOI2012]连连看(费用流)
题目链接 首先是可以\(O(n^2)\)枚举出所有符合要求的点对的,然后考虑建图. 还是拆点把每个点拆成入点和出点,源点连入点,出点连汇点,流量都是1,费用都是0. 然后对于没对符合要求的\((x,y ...
- hihoCoder #1175 : 拓扑排序·二
题目链接:http://hihocoder.com/problemset/problem/1175 代码实现如下: #include <queue> #include <cstdio ...
- 快速幂取模_C++
一.题目背景 已知底数a,指数b,取模值mo 求ans = ab % mo 二.朴素算法(已知可跳过) ans = 1,循环从 i 到 b ,每次将 ans = ans * a % mo 时间复杂度O ...
- c语言学习笔记.数组.
数组: 可以存储一个固定大小的相同类型元素的顺序集合,比如int类型的数组.float类型的数组,里面存放的数据称为“元素”. 所有的数组都是由连续的内存位置组成.最低的地址对应第一个元素,最高的地址 ...
- Python面向对象学习 1 (什么是面向对象,面向对象的应用场景,待更新)
程序设计的三种基本结构: 面向对象,面向过程,函数式编程 1,什么是面向对象编程 面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就 ...
- 78.PL和PS通过BRAM交互共享数据
本篇文章目的是使用Block Memory进行PS和PL的数据交互或者数据共享,通过zynq PS端的Master GP0端口向BRAM写数据,然后再通过PS端的Mater GP1把数据读出来,将结果 ...
- 121.Best Time to Buy and Sell Stock---dp
题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 题目大意:给出一串数组,找到差值最大的差 ...
- centos上git搭建
1 git的安装需要一些包: yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-Ex ...
- 对于ntp.conf的理解
允许与我们的时间源同步时间,但是不允许源查询或修改这个系统上的服务. # Permit time synchronization with our time source, but do not # ...