当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- String palindrome hash template question o (1) judge whether the string is palindrome
- OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
- (C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
- [ybtoj advanced training guide] similar string [string] [simulation]
- 【工控老马】西门子PLC Siemens PLC TCP协议详解
- Calculate the maximum path sum of binary tree
- MySQL indexes and transactions
- CDA data analysis -- Introduction and use of aarrr growth model
- 浏览器存储方案
- BOM DOM
猜你喜欢

High performance erasure code coding

使用Sqoop把ADS层数据导出到MySQL

Find the common ancestor of any two numbers in a binary tree

深拷貝 事件總線

深拷贝 事件总线

高性能纠删码编码

Docker compose configuration mysql, redis, mongodb

This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
![2.6 using recursion and stack - [tower of Hanoi problem]](/img/fc/45038170dafd104691c93716b103cf.jpg)
2.6 using recursion and stack - [tower of Hanoi problem]

Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
随机推荐
Maximum profit of jz63 shares
MySQL and PostgreSQL methods to grab slow SQL
计算二叉树的最大路径和
Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
[I'm a mound pytorch tutorial] learning notes
Calculate the maximum path sum of binary tree
CDA数据分析——AARRR增长模型的介绍、使用
Deep Copy Event bus
Record the range of data that MySQL update will lock
Leetcode122 the best time to buy and sell stocks II
MySQL与PostgreSQL抓取慢sql的方法
Sweetheart leader: Wang Xinling
Shuttle encapsulated AppBar
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Gaode map test case
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
AI中台技术调研
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
The second composition template of postgraduate entrance examination English / chart composition, English chart composition is enough
Leetcode922 sort array by parity II