DP解编辑距离

#include <iostream>
using namespace std;

char *a = "ALGORITHM";
char *b = "ALTRUISTIC";
int dp[20][20];

int main(void)
{
	dp[0][0] = 0;
	int len1 = strlen(a), len2 = strlen(b);
	
	for(int i = 1; i <= len1; ++i)
		dp[0][i] = i;
	for(int i = 1; i <= len2; ++i)
		dp[i][0] = i;	
	
	for(int i = 1; i <= len1; ++i)
		for(int j = 1; j <= len2; ++j)
			dp[i][j] = min(min(dp[i-1][j] + 1, dp[i][j-1] + 1), dp[i-1][j-1] + (a[i-1] == b[j-1] ? 0 : 1));

	cout << dp[len1][len2] << endl;
	return 0;
} 

你可能感兴趣的:(DP解编辑距离)