最短编辑距离

数据结构与算法之最短编辑距离 阅读详情

51nod 1183 编辑距离
题意:插入或删除或修改最少的字符,使两个字符串相同
设d[i][j]表示字符串a[1~i]和b[1~j]的最小编辑距离,则
这里写图片描述
注意边界。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
const int INF=0x3f3f3f3f;
char a[maxn+5];
char b[maxn+5];
int d[maxn+5][maxn+5];

int dp(int i,int j){
    if(i<1)return j;
    if(j<1)return i;
    int &ans=d[i][j];
    if(ans!=-1)return ans;
    ans=INF;
    if(a[i]==b[j])ans=min(ans,dp(i-1,j-1));
    else ans=min(ans,1+min(dp(i-1,j-1),min(dp(i-1,j),dp(i,j-1))));
    return ans;
}

int main(){
    scanf("%s%s",a+1,b+1);
    memset(d,-1,sizeof(d));
    cout<<dp(strlen(a+1),strlen(b+1));
}
DP算法之最短编辑距离 题目描述 给定两个字符串A和B,现在要将A经过若干操作变为B,可进行的操作有: 删除–将字符串A中的某个字符删除。 插入–在字符串A的某个位置插入某个字符。 替换–将字符串A中的某个字符替换为另一个字符。 现在请你求出,将A变为B至少需要进行多少次操作。 输入格式 第一行包含整数n,表示字符串A的长度。 第二行包含一个长度为n的字符串A。 第三行包含整数m,表示字符串B的长度。 第四行包含一个... 阅读详情

相关推荐

最短编辑距离-动态规划

最小编辑距离 更新:2013-11-08 ////////////////////////////////////////////////////////////////////// 问题  有两个字符串a和b。现在对这两个字符串的许可编辑方法有:一、将一个字  符替换成另一个字符,二、添加一个字符、三、删除一个字符。  如何用许可的编辑方法,经过最小次数(距离)地编辑a串,使a串

海的味道 1893

最短编辑距离(Edit Distance)

1.问题描述 有两个字符串S1, S2;编辑距离指将S1转化为S2(或S2转化为S1)所需要的操作次数;这里的操作次数指插入一个字符、删除一个字符或者替换一个字符(有时替换字符不当作是一次操作,会被当成先删除,再插入两次操作); 最短编辑距离:指将字符串S1转化为S2的所需的最小操作次数 2.状态转移方程 3.推导过程 对于一个串S1来说,将其转化为S2: 1.ad-...

Fujh-nk的博客 1069

离合器KP点自学习:从原理到工程实践的全景解析

本文全面解析了离合器KP点自学习的原理与工程实践,详细介绍了KP点的物理本质、控制价值及自学习方法。通过扭矩增量式和转速变化式学习法,结合不同变速箱构型的适配策略,提升离合器控制的精度与效率。文章还探讨了液压系统迟滞补偿和摩擦材料老化应对方案,为工程实践提供可靠保障。

weixin_33717117的博客 287

动态规划——最短编辑距离

将字符串从末尾开始比较有以下递推公式。

Zhang_Qin123的博客 1654

Android:Google三方库之Adjust集成详细步骤

Android为APP添加google adjust功能

学习就是我的个性 4474

Surface Go 2 变身 Linux 开发本:保姆级 Ubuntu 20.04 双系统安装与驱动配置全攻略

本文详细介绍了如何将 Surface Go 2 变身为高效的 Linux 开发本,涵盖 Ubuntu 20.04 双系统安装、专用内核配置、驱动优化等全流程。通过 linux-surface 内核安装和硬件驱动优化,确保 Surface Go 2 的触控、笔输入、摄像头等功能完美运行,适合开发者打造便携生产力工具。

weixin_30596165的博客 616

最短编辑距离问题

一、问题分析 1.1 问题描述 编辑距离是针对二个字符串的差异程度的量化量测,量测方式是看至少需要多少次处理才能将一个字符串变成另一个字符串。编辑距离可以用在自然语言处理中,最短编辑距离算法,用于对用户输入的查询进行纠错,从而优化查询结果。例如拼写检查可以根据一个拼错的字和其他正确的字的编辑距离,判断哪一个(或哪几个)是比较可能的字。 具体点来说,什么是最短编辑距离呢?假定有两个字符串l1和l2,允许对字符串进行以下三种操作: 1. 插入一个字符 2. 删除一个字符 3. 替换一个字符 将字.

yu_peiying的博客 2686

数据分析项目:直播电商用户流失分析

其中,'Tenure'(在网时长)可能是影响用户流失的关键因素,意味着用户在平台上的停留时间越长,流失的可能性可能越低,这提示平台应注重提高用户粘性,增加用户在平台上的活跃度和停留时间。)的分析,挖掘用户流失的原因和模式,分析发现用户投诉处理效率(Complain)、距上次消费时长(DaySinceLastOrder)、会员生命周期(Tenure)等8项核心指标对流失预测贡献度达87%,模型准确率94.58%,并提出相应的建议。重视用户的满意度,及时处理用户的投诉和反馈,不断改进平台的服务和功能。

Nervousr的博客 2459

最短编辑距离 python_最短编辑距离算法实现

一,算法介绍在CS124课程的第一周提到 求解两个字符串相似度的算法---Minimum Edit Distance(最短编辑距离)算法。该算法在NLP(自然语言处理)中也会用到。如何定义相似度呢?任给两个字符串X 和Y,使用以下三种操作将 字符串X 变到 字符串Y :①插入(Insert)操作;②删除操作(delete);③替换操作(substitute)比如 字符串X="intention"...

weixin_39797381的博客 357

30天入门 SAP BTP 开发系列之二:了解 SAP BTP Region 的概念,包含账号申请的详细步骤

SAP BTP 的 Region,实际是一组按地理位置划分的、由 SAP 或合作 Hyperscaler 承载的基础设施栈。这些基础设施栈至少包含多可用区网络、对象存储、计算节点及自动化运维等服务。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1461

第069篇:浸入边界法进阶

浸入边界法(Immersed Boundary Method, IBM)是处理流固耦合问题的强大数值方法。本文深入介绍直接力法、反馈力法、Ghost Cell方法和多域IBM等高级技术。通过Python实现完整的IBM求解器,展示处理移动边界、弹性边界和多体相互作用的方法。浸入边界法,直接力法,反馈力法,Ghost Cell,流固耦合,移动边界,弹性边界Navier-Stokes方程添加体积力项:∂u∂t+(u⋅∇)u=−1ρ∇p+ν∇2u+f\frac{\partial \mathbf{u}}{\part

2401_87715305的博客 412

最短编辑距离(线性dp)-java

最短编辑问题也是一种非常经典的二维线性dp。

m0_63267251的博客 1594

修改 Alist 密码时报错?看看这个坑怎么解决!

启动服务后,再修改密码”。以后再遇到类似问题,先检查服务是否正常运行,说不定就能少踩不少坑。如果还有其他 Alist 的问题,欢迎在评论区留言交流~

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 1154

【C++】【算法基础】最短编辑距离

给定两个字符串长度为n的A和长度为m的B,现在要将A经过若干操作变为B,可进行的操作有:删除——将字符串A中的某个字符删除。插入——在字符串A的某个位置插入某个字符。替换——将字符串A中的某个字符替换为另一个字符。现在请你求出,将A变为B至少需要进行多少次操作。

Dusxin的博客 1019

自私兽群优化器(SHO)详解:从生物本能到智能优化

自私兽群优化器(SHO)是一种受动物群体自私行为启发的智能优化算法。该算法模拟动物在捕食威胁下的两种行为:聚集行为(全局搜索)和自私行为(局部搜索),将优化问题的解空间映射为生存环境,候选解视为猎物个体。SHO通过安全性评估将猎物分为安全、危险和高危三类,分别采用不同的位置更新策略,并引入捕食者角色增加选择压力。相比传统优化算法,SHO具有无需梯度信息、自适应平衡探索与开发、参数简单等优势,特别适合高维复杂和多模态优化问题。算法通过马尔可夫链理论保证收敛性,已在工程设计和数据挖掘等领域展现出良好性能。

qq_42212808的博客 196

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

最短编辑距离问题理解

最短编辑距离是指两个字符串,把其中一个字符串转为另一个字符串所需要花费的最小操作成本。 设dp[i][j]为Xi与Yj的最短编辑距离,则Xi与Yj处于最优解时的排列有三种情况 1.Xi最后一个元素xi位于Yj最后一个元素yj的左边 2.Xi最后一个元素xi位于Yj最后一个元素yj的右边 3.Xi最后一个元素xi与Yj最后一个元素yj重合 无论最终dp[i][j]的字符串如何对齐,只用取三...

weixin_30922589的博客 124

VB摄像头监控

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

2020年华为春招真题(最短编辑距离)详解+Java代码实现(校招,Minimum Edit Distance,Levenshtein距离,代码,原理解释)

一、问题背景 博主最近在准备2020年的软件工程编程领域的春招,其中华为提供了博主上机考试的机会,因此博主参考了2020年3月4日的华为校园春招的真题,其中真题的最后一题便是最短编辑距离的改编题,但博主我不清楚此题的具体实现方法,于是对此在国内外网站上进行了详细的调查和研究,下面为最短编辑距离的原理解释与Java代码实现。 其中2020年3月4日的华为校园春招的真题第3题——编辑距离改编题如下: ...

快乐李同学的博客 2450

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

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

深度学习工具包 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")

算法基础之最短编辑距离

​ 因为在求a[i-1]时 已经从。删掉a[i]就能使a[i-1] 和 b[j] 相等?集合定义 : f[i][j]为操作方式的最小值。集合计算 : 三种操作 取最小。核心思想 : 线性dp。

Pisasama的博客 646
上一篇: 错误信息:Address already in use: connect
下一篇: win7安装到gpt分区
FightingForFinal
博客等级 码龄11年 24粉丝 100原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值