当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Go learning notes - go based interprocess communication
- Leetcode - Sword finger offer 51 Reverse pairs in an array
- What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
- SSH automatically disconnects (pretends to be dead) after a period of no operation
- In development, why do you find someone who is paid more than you but doesn't write any code?
- Anti shake throttle
- AI中台技术调研
- Go learning notes - multithreading
- Record the range of data that MySQL update will lock
- 高性能纠删码编码
猜你喜欢

AI中台技术调研

mysql表的增删改查(进阶)

Jenkins user rights management

【工控老马】西门子PLC Siemens PLC TCP协议详解

Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)

mysql数据库基础

Sparkcontext: error initializing sparkcontext solution

drools决策表的简单使用

MySQL and PostgreSQL methods to grab slow SQL

中国交通标志检测数据集
随机推荐
2.6 using recursion and stack - [tower of Hanoi problem]
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
Openssh remote enumeration username vulnerability (cve-2018-15473)
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
CDH6之Sqoop添加数据库驱动
lombok常用注解
Calculate the maximum path sum of binary tree
Maximum profit of jz63 shares
2.7 binary tree, post order traversal - [FBI tree]
H5 to app
arcgis js 4.x 地图中加入图片
Adding database driver to sqoop of cdh6
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
BOM DOM
Differences between nodes and sharding in ES cluster
mysql数据库基础
Sort---
Go learning notes - go based interprocess communication
Go learning notes - multithreading
In development, why do you find someone who is paid more than you but doesn't write any code?