当前位置:网站首页>线性DP AcWing 902. 最短编辑距离
线性DP AcWing 902. 最短编辑距离
2022-07-02 09:43:00 【T_Y_F666】
线性DP AcWing 902. 最短编辑距离
原题链接
算法标签
动态规划 线性DP
思路

代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 1005, INF = 0x3f3f3f3f;
int n,m;
char A[N], B[N];
int f[N][N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
n=read();
scanf("%s", A+1);
m=read();
scanf("%s", B+1);
// 若A为空 B有i个字符 进行i次添加操作
rep(i, 0, m+1){
f[0][i]=i;
}
// 若A有i个字符 B为空 进行i次删除操作
rep(i, 0, n+1){
f[i][0]=i;
}
rep(i, 1, n+1){
rep(j, 1, m+1){
f[i][j]=min(f[i-1][j], f[i][j-1])+1;
if(A[i]==B[j]){// 无需替换操作
f[i][j]=min(f[i][j], f[i-1][j-1]);
}else{
f[i][j]=min(f[i][j], f[i-1][j-1]+1);
}
}
}
printf("%lld", f[n][m]);
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- drools执行String规则或执行某个规则文件
- FastDateFormat为什么线程安全
- Leetcode - Sword finger offer 37, 38
- Mysql database foundation
- 2.6 using recursion and stack - [tower of Hanoi problem]
- (C language) octal conversion decimal
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
- 记录一下MySql update会锁定哪些范围的数据
- Openssh remote enumeration username vulnerability (cve-2018-15473)
- Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
猜你喜欢

Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer

MySQL与PostgreSQL抓取慢sql的方法

MySQL indexes and transactions

Simple understanding of ThreadLocal

Distributed machine learning framework and high-dimensional real-time recommendation system

高性能纠删码编码

浏览器存储方案

This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry

PR 2021 quick start tutorial, learn about the and functions of the timeline panel

mysql表的增删改查(进阶)
随机推荐
Fastdateformat why thread safe
Leetcode14 longest public prefix
mysql索引和事务
AI mid stage technology research
Shutter encapsulated button
Anti shake throttle
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Brush questions --- binary tree --2
二分刷题记录(洛谷题单)区间的甄别
Adding database driver to sqoop of cdh6
2.6 using recursion and stack - [tower of Hanoi problem]
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
AI中台技术调研
drools动态增加、修改、删除规则
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
drools执行指定的规则
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
上传文件时,服务器报错:IOFileUploadException: Processing of multipart/form-data request failed. 设备上没有空间
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?