当前位置:网站首页>Linear DP acwing 902 Shortest editing distance
Linear DP acwing 902 Shortest editing distance
2022-07-02 12:43:00 【T_ Y_ F666】
linear DP AcWing 902. The shortest editing distance
Original link
AcWing 902. The shortest editing distance
Algorithm tags
Dynamic programming linear DP
Ideas

Code
#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);
// if A It's empty B Yes i Characters Conduct i Add operations
rep(i, 0, m+1){
f[0][i]=i;
}
// if A Yes i Characters B It's empty Conduct i Delete operations
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]){// No replacement operation required
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;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- 上手报告|今天聊聊腾讯目前在用的微服务架构
- 8 examples of using date commands
- Visual studio efficient and practical extension tools and plug-ins
- Heap acwing 839 Simulated reactor
- 哈希表 AcWing 841. 字符串哈希
- 获取文件版权信息
- Docker compose configuration mysql, redis, mongodb
- JSON serialization and parsing
- Some sudden program ideas (modular processing)
- Redis introduction, scenario and data type
猜你喜欢

High performance erasure code coding

JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)

浏览器存储方案

Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)

arcgis js 4. Add pictures to x map

Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime

Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)

Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand

区间DP AcWing 282. 石子合并
![1380. Lucky numbers in the matrix [two-dimensional array, matrix]](/img/8c/c050af5672268bc7e0df3250f7ff1d.jpg)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
随机推荐
Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
CPU指令集介绍
[I'm a mound pytorch tutorial] learning notes
分布式机器学习框架与高维实时推荐系统
2.7 binary tree, post order traversal - [FBI tree]
Deep copy event bus
JSON serialization and parsing
Oracle从入门到精通(第4版)
spfa AcWing 851. SPFA finding the shortest path
线性DP AcWing 902. 最短编辑距离
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
计数类DP AcWing 900. 整数划分
MySQL and PostgreSQL methods to grab slow SQL
Drools terminates the execution of other rules after executing one rule
. Net wechat message template push
Go learning notes - go based interprocess communication
Shutter encapsulated button
Docsify deploy IIS
Record the range of data that MySQL update will lock
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students