当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
- [ybtoj advanced training guidance] cross the river [BFS]
- Drools executes the specified rule
- drools执行String规则或执行某个规则文件
- Introduction to CPU instruction set
- MySQL与PostgreSQL抓取慢sql的方法
- Adding database driver to sqoop of cdh6
- China traffic sign detection data set
- AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
- Tas (file d'attente prioritaire)
猜你喜欢

Map和Set

The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night

AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning

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

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

PR 2021 quick start tutorial, learn about the and functions of the timeline panel

Embedded Software Engineer career planning

Adding database driver to sqoop of cdh6

Take you ten days to easily finish the finale of go micro services (distributed transactions)

Deep copy event bus
随机推荐
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
【工控老马】西门子PLC Siemens PLC TCP协议详解
drools执行完某个规则后终止别的规则执行
Openssh remote enumeration username vulnerability (cve-2018-15473)
Shuttle encapsulated AppBar
[ybtoj advanced training guidance] cross the river [BFS]
Drools terminates the execution of other rules after executing one rule
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
Addition, deletion, modification and query of MySQL table (Advanced)
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
FBX import under ue4/ue5 runtime
上传文件时,服务器报错:IOFileUploadException: Processing of multipart/form-data request failed. 设备上没有空间
二分刷题记录(洛谷题单)区间的甄别
drools执行指定的规则
Drools executes string rules or executes a rule file
Sweetheart leader: Wang Xinling
Take you ten days to easily finish the finale of go micro services (distributed transactions)
BOM DOM
Leetcode - Sword finger offer 51 Reverse pairs in an array