poj 2185 二维kmp

二维KMP - 求字符矩阵的最小覆盖矩阵 - poj 2185 Milking Grid    Problem's Link:http://poj.org/problem?id=2185   Mean:   给你一个n*m的字符矩阵,让你求这个字符矩阵的最小覆盖矩阵,输出这个最小覆盖矩阵的面积。 analyse: 做了上一篇博客的题目,就会求一个字符串的最小覆盖矩阵。同样的,现在求字符矩阵的最小覆盖矩阵,只是将一维推向了二维,我们在纸上画一下图, 阅读详情

题意:求最小的模式块,使其无限扩展后包含给你的矩阵块(看别人题解才懂的题意);

分析:假设存在一个模式块可以满足上述条件,那么必然存在一个起点在(0,0)的模式块满足上述条件;

对于每一行,我们找出所有可以满足条件的前缀记录下长度,那么满足所有行的最短的长度就是该模式块的宽r;

对于模式块的长,我们把宽r的字符串压缩看出一个字符,然后再进行KMP,找出该字符串的最小循环串,即长l;

答案就是r * l;

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>

#define INF 0x3f3f3f3f
#define IMAX 2147483646
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int

using namespace std;

int r, c;
char mp[11111][99];
int cnt[99],nn[11111];

int main() {
	scanf("%d%d", &r, &c);
	for (int i = 1; i <= r; i++)
		scanf("%s", mp[i] + 1);
	
	for (int i = 1; i <= r; i++) {
		nn[1] = 0;
		for (int j = 2, k = 0; j <= c; j++) {
			while (k > 0 && mp[i][k + 1] != mp[i][j])
				k = nn[k];
			if (mp[i][k + 1] == mp[i][j])k++;
			nn[j] = k;
		}
		int tt = c;
		while (tt != 0) {
			cnt[c - nn[tt]]++;
			tt = nn[tt];
		}
	}
	int a;
	for(int i = 1;i <= c;i++)
		if (cnt[i] == r) {
			a = i;
			break;
		}
	nn[1] = 0;
	for (int i = 2, j = 0; i <= r; i++) {
		while (j > 0 && strcmp(mp[i] + 1, mp[j + 1] + 1) != 0)
			j = nn[j];
		if (strcmp(mp[i] + 1, mp[j + 1] + 1) == 0)j++;
		nn[i] = j;
	}
	printf("%d\n", (r - nn[r])*a);
	
	return 0;
}
POJ2185_Milking Grid_KMP_二维求最小覆盖子矩阵 【不能用最小公倍数或最大值求覆盖子矩阵的宽和高!!】 昨天看了很久这个题,去POJ 的论坛上,看到几个分析说网上大多数的AC代码都是错的,只是POJ上的数据过于弱就过了,比如求行方向上最小覆盖的列数(宽度)只要用到求最小公倍数的就都是错的,希望大家做此题的时候注意一下,下面题解的时候会解释为什么是错的 题意: 给一个二维的字符串矩阵,求他的一个最小的子矩阵 阅读详情

相关推荐

KMP 算法:字符串匹配的高效解决方案

KMP 算法作为一种经典的字符串匹配算法,以其独特的利用已匹配信息的思想、稳定的时间复杂度和相对简单的实现方式,在众多领域发挥了重要作用。它在文本处理、生物信息学、数据压缩等领域的应用,为解决实际问题提供了高效的解决方案。

candy的博客 1864

字符串——广义&二维KMP

字符串——广义&二维KMP 广义KMP 普通KMP是关于字符序列的一种模式匹配算法,而广义KMP将字符序列推广到一般序列,即带有等价关系的序列均可以进行广义KMP算法。 即我们只需要定义序列中的元素是否相等或不相等,即可像普通KMP一样执行广义KMP算法。 二维KMP 广义KMP的一种特殊实现就是二维KMP,在二维KMP中的序列元素就是字符串。例如序列{“abcd”,“addfd”,“dwffas”},我们可以定义字符串相等,那么就可以在字符串序列上执行广义KMP算法,即二维KMP算法。 POJ 2

jiahonghao2002的博客 696

手动以及使用torch.nn实现前馈神经网络实验

手动以及使用torch.nn实现前馈神经网络 手动实现前馈神经网络解决上述回归、二分类、多分类任务 利用torch.nn解决上述回归、二分类、多分类任务 在多分类任务中使用至少三种不同的激活函数 对多分类任务中的模型评估隐藏层层数和隐藏单元个数对实验结果的影响

m0_52910424的博客 9372

F. 地图压缩(二维kmp+哈希)

https://codeforces.com/gym/103389/problem/F 实际上比较能看出来是kmp吧。 POJ2185是二维kmp的最小矩阵面积。询问次数多的话就不能每次取字符串遍历了。需要找到一个快速取字符串value的方式就是哈希。 二维的公共前后缀怎么找呢?对每行每列进行哈希的预处理,对于查询的矩阵或者行的哈希值数组。对于列同理。 然后找出行的哈希value的最大公共前后缀长度,即最多可以有多少行。 对列同理找出哈希value的最大公共前后缀长度,即最多可以有多少列。 行数和列数乘积就

代码笔记本 614

从零开始用TS开发babylonjs - 本机环境搭建

babylonjs,初始化本地安装,web3d引擎

han3527638的专栏 794

网易 广州 梦幻西游端游 客户端社招笔试题 2024

4道算法题,时间2小时,牛客网笔试注:采用OJ模式,需要自己处理输入、输出。

z2014z的博客 1813

字符串——奶牛矩阵(二维kmp循环节)

【代码】字符串——奶牛矩阵(二维kmp循环节)

测试 256

openlayers6【十九】vue HeatmapLayer热力图层实现热力图效果详解

文章目录1. 写在前面2. Heatmap 类实现热力图2.1 Heatmap 参数2.2 实现热力图3. 完整代码4. 添加删除map图层的方法5. 热力图自身的get,set方法 1. 写在前面 我们主要看热力图的效果,区域绘制效怎么设置详细内容可以访问 openlayers6【十七】vue VectorLayer矢量图层画地图省市区,多省市区(粤港澳大湾区)效果详解 在 openlayers 中,图层是使用 layer 对象表示的,主要有 WebGLPoints Layer、热度图(HeatMap L

@必意玲 1万+

POJ2185 最小覆盖矩阵 (二维KMP)

这题挺有意思的,主要把KMP理解透彻了,其实二维的可以看成一维的,原来是由单个字符组成的字符串,现在就是由字符串组成的字符串数组,在此基础上,其他操作和思想都是一样的,横向求一遍next数组,那么最小的可覆盖的子串长度为:len-next[len],同理再求一遍纵向的,相乘即可。#include<iostream> #include<stdio.h> #include<string.h> using

qq_33688997的博客 895

get_seed协议

【代码】get_seed协议。

a545958498的博客 911

别再只盯着照片了!手把手教你用Python提取大疆照片里的GPS、姿态和RTK数据

本文详细介绍了如何使用Python提取大疆照片中的GPS、姿态和RTK数据,帮助用户解锁无人机拍摄照片中的隐藏元数据。通过实战代码示例,展示了如何解析EXIF格式的定位、姿态和RTK状态信息,并实现批量处理与数据分析,适用于测绘、农业巡检等专业领域。

weixin_30790841的博客 153

POJ 2185 二维KMP

题意:就是让你求出最小的字符矩阵面积,这个矩阵是这个大矩阵里能够循环的,但是并不一定是全部循环相同的,部分相同也算循环,比如样例。 思路:这题挺好的,以前没有想到二维字符串数组也可以用next数组求出其循环节,现在这题正好补了这个空。 解法:把每一个字符串当做字符进行求next数组,然后求出最小的循环字符串长度,即:len-next[len]。因为以前求循环节是len/(len-next[le

liwei的专栏 1202

【Unity报错】Unity导入海康SDK 报错 DllNotFoundException: PlayCtrl assembly

2.尝试按网上其他的办法(包括使用depengs查看dll依赖等)1.先检查Plugins文件夹中是否存在PlayCtrl文件。3.如果网上查阅的办法都不能解决,请继续往下看。更换低版本的SDK库文件。

weixin_42968584的博客 1126

POJ 2185 Milking Grid 二维kmp

题意:给出一个矩阵,求最小的子矩阵,使得原矩阵包含在子矩阵的某个扩展中。 比如 ABABA ABABA 就是被包含在AB扩展的矩阵中。 输出这个最小的子矩阵的大小。 其实只要两遍KMP就行了,把每行当成一个字符KMP一遍求最小循环子串的规模,然后在那几行子矩阵中把每列矩阵当成一个字符再求一遍最小循环子串。 代码: /* * Author: illuz

水果君の日常 2227

【YoloV11改进策略】基于流形约束超连接(mHC)增强YoloV11全局特征提取能力|独家复现|梁文峰署名论文

论文标题:流形约束超连接 (Manifold-Constrained Hyper-Connections, mHC)核心问题:针对超连接 (Hyper-Connections, HC) 扩展残差流宽度时破坏恒等映射、引发训练不稳定性和高内存开销的问题,提出一种约束性框架。目标:恢复残差连接的稳定性与可扩展性,同时保持HC的性能增益。基准模型:基于DeepSeek-V3的MoE架构,进行了3B、9B、27B模型的实验验证。论文图示mHC 是一个通用的HC扩展框架。

m0_47867638的博客 1349

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Poj--2185(KMP,二维)

2014-12-1301:00:47 思路:(吐槽下网上有些题解是的.....从百度文库打印的题解就错了orz...最后还是自己YY出的)   用KMP求出每行的最小重复字符串长度,求法就是总元素个数 - 最后一个字符的P[]值(与next数组不同概念,这里应该叫覆盖...)   比如:ABCAB,P[5]=2(假如下标从1开始),那么最小重复字符串长度为5-P[5] = 5-2...

dingdi3021的博客 169

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

POJ 2185 Milking Grid 二维KMP

点击打开链接 题意:R*C字符矩阵,R 若为一维,则相当于用kmp求出最小循环节(只要求覆盖,len%(len-next[len])可以不为0) 先对每行求最小循环节xi,子矩形的宽度最小为:xi(i=1~r)的最小公倍数lr. 在对每列求最小循环节yi 子矩形的高度最小为:yi(i=1~c)的最小公倍数lc. 以lr*lc子矩阵为单位显然能覆盖整个字符矩阵 #include #

Masamiiiiii 526

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

POJ 2185 二维kmp算法

http://poj.org/problem?id=2185 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

ACM,再见! 1401

poj2185 Milking Grid 二维kmp

给一个字符矩阵,求一个最小的矩阵,使得这个矩阵复制若干次可以完整包含原矩阵。首先考虑行,找出每行的最小重复单元的长度后,他们的最小公倍数就是横向上的最小单元长度;同理纵向上求出纵向上的最小单元长度,要注意的一点是如果这两个长度大于n或m得话,就取n或m,因为最长也就是他们本身的长度..那么现在的问题就是求一行(或一列)的最小重复单元了,求出该行的失配函数,len-f[len]就是这一行的最小重复单

Broken Wings 852

POJ_2185_二维KMP

http://poj.org/problem?id=2185 求最小覆盖矩阵,把KMP扩展到二维,行一次,列一次,取最小覆盖线段相乘即可。 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespa...

diaotan0836的博客 103

POJ 2185 Milking Grid(二维KMP)

超级传送门:http://poj.org/problem?id=2185 这道题网上很多其他的blog上的题解都是错的,由于题目自身数据比较水,便都混过去了。其中一种典型的错误解法是求出每一行/列的最小覆盖长度(ABCAB的最小覆盖长度就是3,即对应ABC),然后LCM,若结果大于串长,则取串长。这是一种典型的错误贪心,诸如: 2 8 ABCDEFAB AAAABAAA 这组数据

Uranux 1232

poj 2185 Milking Grid(最小矩阵覆盖,二维 kmp 匹配)

题目:http://poj.org/problem?id=2185 题目大意:给你一个二维字符串矩阵,问你最小的覆盖矩阵的大小。 思路:好题啊,绝对的kmp好题!具体解题思想可以参考这个网址:http://blog.sina.com.cn/s/blog_69c3f0410100tyjl.html,这个才是真犀利,先暴力求出每行的可能 的子串,然后选择一个最小的,那么着肯定是最小的列数,然后对

沉溺的专栏 897

POJ 2185 KMP 二维字符串找最短循环矩阵

主要思想就是降维处理。 相似题目: HDU 3746:一维字符串找最短循环节,这道题是HDU3746的升级版,增加了一个维度。 题意: 一个字符矩阵看成一个地面,然后把最短循环矩阵看成瓷砖,需要铺满地面,地面的边缘部分,瓷砖可以残缺。 题解: 我们不防把最短循环矩阵拆分成,行的最短循环节,列的最短循环节。 还是直接举例子吧,比较清楚些。 A B A ...

shadandeajian的博客 350

Match:Milking Grid(二维KMP算法)(POJ 2185)

                                     奶牛矩阵   题目大意:给定一个矩阵,要你找到一个最小的矩阵,这个矩阵的无限扩充的矩阵包含着原来的矩阵   思路:乍一看这一题确实很那做,因为我们不知道最小矩阵的位置,但是仔细一想,如果我们能把矩阵都放在左上角该多好,这样一来这一题好像又是循环数组那个样子了(二维的)。   而事实上我们确实可以把所有情况都...

bandilan2400的博客 230
上一篇: poj 2965
下一篇: poj 3630 字典树
FawkesLi
博客等级 码龄8年 21粉丝 170原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值