编辑距离问题

#include 
using namespace std;
int main(){
	char A[] = "fxpimu";
	char B[] = "xwrs";
	int lena = strlen(A);
	int lenb = strlen(B);
	int **m = new int*[lena+1];
	for (int i = 0; i <= lena; i++){
		m[i] = new int[lenb+1];
	}
	for (int i = 0; i <= lena; i++){
		m[i][0] = i;
	}
	for (int j = 0; j <= lenb; j++){
		m[0][j] = j;
	}
	for (int i = 1; i <= lena; i++){
		for (int j = 1; j <= lenb; j++){
			int del = m[i - 1][j]+1;
			int ins = m[i][j - 1]+1;
			int tihuan = m[i - 1][j - 1] + (A[i] == B[j] ? 0 : 1);
			m[i][j] = del < ins ? del : ins;
			m[i][j] = m[i][j] < tihuan ? m[i][j] : tihuan;
		}
	}
	cout << m[lena][lenb] << endl;
	return 0;
}

你可能感兴趣的:(算法分析)