当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 字符串回文hash 模板题 O(1)判字符串是否回文
- MySQL and PostgreSQL methods to grab slow SQL
- Drools dynamically add, modify, and delete rules
- Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
- Jenkins user rights management
- CDA data analysis -- Introduction and use of aarrr growth model
- mysql索引和事务
- Full link voltage measurement
- The blink code based on Arduino and esp8266 runs successfully (including error analysis)
- kubenetes中port、targetPort、nodePort、containerPort的区别与联系
猜你喜欢
Adding database driver to sqoop of cdh6
Sort---
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
BOM DOM
BOM DOM
Drools dynamically add, modify, and delete rules
Brush questions --- binary tree --2
Tas (file d'attente prioritaire)
mysql数据库基础
随机推荐
Input a three digit number and output its single digit, ten digit and hundred digit.
LeetCode—<动态规划专项>剑指 Offer 19、49、60
BOM DOM
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
Find the common ancestor of any two numbers in a binary tree
Drools executes string rules or executes a rule file
Docker compose configuration mysql, redis, mongodb
Drools terminates the execution of other rules after executing one rule
SparkContext: Error initializing SparkContext解决方法
MySQL indexes and transactions
Distributed machine learning framework and high-dimensional real-time recommendation system
寻找二叉树中任意两个数的公共祖先
Sse/avx instruction set and API of SIMD
[FFH] little bear driver calling process (take calling LED light driver as an example)
mysql数据库基础
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
Jenkins voucher management
堆(優先級隊列)
CDA数据分析——Excel数据处理的常见知识点归纳
Drools executes the specified rule