洛谷 P1279 字串距离

题目传送门

\(字符串dp,f_{i,j}表示a串到第i位和b串到第j位的最小距离,转移和LCS一样\)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath> 

using namespace std;

string a,b;
int f[2001][2001],n;

int main() {
	memset(f,0x3f3f3f3f,sizeof(f));
	cin >> a >> b;
	scanf("%d",&n);
	int lena = a.length();
	int lenb = b.length();
	for(int i = 0;i <= lena; i++) f[i][0] = n * i;
	for(int i = 0;i <= lenb; i++) f[0][i] = i * n;
	for(int i = 1;i <= lena; i++)
		for(int j = 1;j <= lenb; j++)
			f[i][j] = min(f[i-1][j] + n,min(f[i][j-1] + n,f[i-1][j-1] + abs(a[i-1] - b[j-1])));
	printf("%d",f[lena][lenb]);
	return 0;
}
posted @ 2020-09-14 09:09  Mr^Simon  阅读(62)  评论(0编辑  收藏  举报