当前位置:网站首页>最短编辑距离(线性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;
}
边栏推荐
- Research Report on the overall scale, major producers, major regions, products and application segments of swine vaccine in the global market in 2022
- FileOutputStream
- 政策关注 | 加快构建数据基础制度,维护国家数据安全
- c/s 架构
- 等等, 怎么使用 SetMemoryLimit?
- Unity shader learning (II) the first shader
- In 2021, the global enhanced oil production surfactant revenue was about USD 202.3 million, and it is expected to reach USD 297.1 million in 2028
- [tcapulusdb knowledge base] Introduction to tmonitor system upgrade
- Unity Shader学习(一)认识unity shader基本结构
- Youboxun attended the openharmony technology day to create a new generation of secure payment terminals
猜你喜欢

政策关注 | 加快构建数据基础制度,维护国家数据安全

Jwas: a Bayesian based GWAS and GS software developed by Julia

Research Report on the overall scale, major producers, major regions, products and application segments of swine vaccine in the global market in 2022

matlab习题 —— 创建 50 行 50 列全零矩阵、全 1 矩阵、单位矩阵、对角矩阵,输出矩阵第135号元素。
![[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (I)](/img/04/b1194ca3340b23a4fb2091d1b2a44d.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (I)
![[tcaplusdb knowledge base] Introduction to tcaplusdb tcaplusadmin tool](/img/ba/f865c99f3ea9e42c85b7e906f4f076.png)
[tcaplusdb knowledge base] Introduction to tcaplusdb tcaplusadmin tool

Youboxun attended the openharmony technology day to create a new generation of secure payment terminals

C語言0長度數組的妙用

QStyle实现自绘界面项目实战(一)

Unity Shader学习(一)认识unity shader基本结构
随机推荐
The R language uses the follow up The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The labels parameter adds label information t
最大路径和问题(摘樱桃问题)
MQTT协议栈原理及交互流程图
杰理之增加一个输入捕捉通道【篇】
Youboxun attended the openharmony technology day to create a new generation of secure payment terminals
Popular science of device review: popular science of innovative medical device series - sternum plate products
器审科普:创新医疗器械系列科普——胸骨板产品
Detailed explanation of interprocess communication
旭日3SDB,安装原版ros
AUTOCAD——三种修剪方式
Mqtt protocol stack principle and interaction flow chart
2022CISCN华中 Web
"24 of the 29 students in the class successfully went to graduate school" rushed to the hot search! Where are the remaining five?
Build the Internet of things system from scratch
alibaba jarslink
Jerry's serial port communication serial port receiving IO needs to set digital function [chapter]
杰理之睡眠以后定时唤醒系统继续跑不复位【篇】
进程间通信详解
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(一)
Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022