当前位置:网站首页>最短编辑距离(线性dp写法)
最短编辑距离(线性dp写法)
2022-06-27 11:33:00 【吴雨4】
题面:
思路:
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1010;
ll n,m;
char a[N],b[N];
int f[N][N];
int main()
{
cin>>n;
cin>>a+1;
cin>>m;
cin>>b+1;
//边界处理当f[n][0]时需要的步骤是删除n步a有的元素
//同理f[0][m]时需要的步骤是删除m步b有的元素
for(int i=0;i<=n;i++) f[i][0]=i;
for(int i=0;i<=m;i++) f[0][i]=i;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
f[i][j]=min(f[i-1][j]+1,f[i][j-1]+1);//比较增删的操作数取小值
//这里是替换的操作,判断是否相同;
if(a[i]==b[j]) f[i][j]=min(f[i][j],f[i-1][j-1]);//相同的话这不需要操作数+1(不需要替换)
//不同则需要加上替换的操作数1
else f[i][j]=min(f[i][j],f[i-1][j-1]+1);
}
}
cout<<f[n][m]<<endl;
return 0;
}
边栏推荐
- In 2021, the global professional liability insurance revenue was about USD 44740million, and it is expected to reach USD 55980million in 2028. From 2022 to 2028, the CAGR was 3.5%
- R language dplyr package arrange function sorts dataframe data, sorts dataframe data through multiple data columns, specifies the first field to be sorted in descending order, and does not specify the
- Code for structural design of proe/creo household appliances - electric frying pan
- On ticheck
- Unity shader learning (I) understanding the basic structure of unity shader
- C# wpf 实现撤销重做功能
- Llvm family (1) - Introduction to llvm
- Unity shader learning (II) the first shader
- Qstype implementation of self drawing interface project practice (I)
- R language uses the polR function of mass package to construct the ordered multi classification logistic regression model, and uses the vglm function of VGAM package to test the parallelism hypothesis
猜你喜欢
1. Mx6ull startup mode
面试突击60:什么情况会导致 MySQL 索引失效?
【TcaplusDB知识库】TcaplusDB常规单据介绍
【TcaplusDB知识库】TcaplusDB单据受理-创建游戏区介绍
Challenges of machine learning system in production
Code for structural design of proe/creo household appliances - electric frying pan
The wonderful use of 0 length array in C language
dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(一)
【TcaplusDB知识库】TcaplusDB单据受理-建表审批介绍
随机推荐
Peak store app imitation station development play mode explanation source code sharing
AUTOCAD——三种修剪方式
Informatics Olympiad all in one 1332: [example 2-1] weekend dance
Jianmu continuous integration platform v2.5.0 release
Safe landing practice of software supply chain under salesforce containerized ISV scenario
QStyle类用法总结(二)
R语言dplyr包arrange函数排序dataframe数据、通过多个数据列排序dataframe数据、指定第一个字段降序排序,第二字段不指定(默认升序排序)
[tcapulusdb knowledge base] tcapulusdb business data backup introduction
[worthy of collection] centos7 installation MySQL complete operation command
【值得收藏】Centos7 安装mysql完整操作命令
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
旭日3SDB,安装原版ros
C語言0長度數組的妙用
R language dplyr package arrange function sorts dataframe data, sorts dataframe data through multiple data columns, specifies the first field to be sorted in descending order, and does not specify the
I.MX6ULL启动方式
Microsoft cloud technology overview
Unity shader learning (I) understanding the basic structure of unity shader
FileOutputStream
R语言glm函数构建二分类logistic回归模型(family参数为binomial)、使用AIC函数比较两个模型的AIC值的差异(简单模型和复杂模型)
【TcaplusDB知识库】TcaplusDB系统管理介绍