0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

902. 最短编辑距离

https://www.acwing.com/problem/content/submission/code_detail/10658676/
This commit is contained in:
Baoshuo Ren 2022-02-07 10:55:47 +08:00
parent 8cb75eb6bd
commit fb2ad001de
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -1,28 +1,26 @@
#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
using namespace std;
using std::cin;
using std::cout;
#define endl '\n'
int n, m, f[1005][1005];
string a, b;
const int N = 1005;
int n, m, f[N][N];
char a[N], b[N];
int main() {
cin >> n >> a >> m >> b;
a = ' ' + a;
b = ' ' + b;
for (int i = 0; i <= n; i++) {
cin >> n >> a + 1 >> m >> b + 1;
for (int i = 1; i <= n; i++) {
f[i][0] = i;
}
for (int i = 0; i <= m; i++) {
for (int i = 1; i <= m; i++) {
f[0][i] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
f[i][j] = min(f[i - 1][j] + 1, 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);
}
f[i][j] = std::min({f[i - 1][j] + 1, f[i][j - 1] + 1, f[i - 1][j - 1] + (a[i] != b[j])});
}
}
cout << f[n][m] << endl;