当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
猜你喜欢
BOM DOM
2.7 binary tree, post order traversal - [FBI tree]
Map和Set
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
中国交通标志检测数据集
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
Embedded Software Engineer career planning
mysql表的增删改查(进阶)
(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?
High performance erasure code coding
随机推荐
arcgis js 4.x 地图中加入图片
Input box assembly of the shutter package
Intel internal instructions - AVX and avx2 learning notes
MySQL indexes and transactions
Map和Set
Multiply LCA (nearest common ancestor)
怎样写一篇赏心悦目的英文数学论文
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
CPU指令集介绍
arcgis js 4. Add pictures to x map
Jenkins voucher management
Is the neural network (pinn) with embedded physical knowledge a pit?
Drools dynamically add, modify, and delete rules
Performance tuning project case
drools中then部分的写法
BOM DOM
Distributed machine learning framework and high-dimensional real-time recommendation system
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
Deep understanding of P-R curve, ROC and AUC
Openssh remote enumeration username vulnerability (cve-2018-15473)