当前位置:网站首页>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
边栏推荐
- 一些突然迸发出的程序思想(模块化处理)
- Adding database driver to sqoop of cdh6
- Drools executes string rules or executes a rule file
- H5 to app
- Leetcode - Sword finger offer 51 Reverse pairs in an array
- 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
- AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
- Simple understanding of ThreadLocal
- Bom Dom
- China traffic sign detection data set
猜你喜欢
ArrayList与LinkedList效率的对比
bellman-ford AcWing 853. 有边数限制的最短路
JDBC prevent SQL injection problems and solutions [preparedstatement]
Rust search server, rust quick service finding tutorial
AI mid stage technology research
Adding database driver to sqoop of cdh6
中国交通标志检测数据集
China traffic sign detection data set
Embedded Software Engineer career planning
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
随机推荐
哈希表 AcWing 840. 模拟散列表
区间DP AcWing 282. 石子合并
Execute any method of any class through reflection
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
Record the range of data that MySQL update will lock
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
Leetcode - Sword finger offer 51 Reverse pairs in an array
深拷贝 事件总线
Adding database driver to sqoop of cdh6
绕过ObRegisterCallbacks需要驱动签名方法
Go learning notes - multithreading
Go learning notes - go based interprocess communication
防抖 节流
Find the common ancestor of any two numbers in a binary tree
IPhone 6 plus is listed in Apple's "retro products" list
线性DP AcWing 898. 数字三角形
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
Oracle从入门到精通(第4版)
模块化 CommonJS ES Module
8 examples of using date commands