当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Lombok common annotations
- Go学习笔记—多线程
- kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
- (C language) octal conversion decimal
- Leetcode14 longest public prefix
- 使用Sqoop把ADS层数据导出到MySQL
- Leetcode topic [array] -540- single element in an ordered array
- 【工控老马】西门子PLC Siemens PLC TCP协议详解
- The differences and relationships among port, targetport, nodeport and containerport in kubenetes
- Day12 control flow if switch while do While guessing numbers game
猜你喜欢

CDA数据分析——AARRR增长模型的介绍、使用

AI mid stage technology research

Tas (file d'attente prioritaire)

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

中国交通标志检测数据集

Brush questions --- binary tree --2
![[ybtoj advanced training guidance] cross the river [BFS]](/img/4e/83f14452ea6410768cdd01e725af2e.jpg)
[ybtoj advanced training guidance] cross the river [BFS]

Map and set

CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节

分布式机器学习框架与高维实时推荐系统
随机推荐
Docker-compose配置Mysql,Redis,MongoDB
Test shift left and right
(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?
Full link voltage measurement
How to write a pleasing English mathematical paper
堆(優先級隊列)
Use sqoop to export ads layer data to MySQL
drools决策表的简单使用
drools中then部分的写法
"As a junior college student, I found out how difficult it is to counter attack after graduation."
High performance erasure code coding
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
高性能纠删码编码
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
BOM DOM
Take you ten days to easily finish the finale of go micro services (distributed transactions)
lombok常用注解
arcgis js 4. Add pictures to x map
记录一下MySql update会锁定哪些范围的数据