POJ 3356 AGTC (最小编辑距离)

#include <stdio.h>
#define MAX_LEN 1010
#define MIN(x, y) ( (x) < (y) ? (x) : (y) )
int xLen;
char x[MAX_LEN];
int yLen;
char y[MAX_LEN];
int dist[MAX_LEN][MAX_LEN];

int main(){
	//注意是多case
	while (scanf("%d%s%d%s", &yLen, y, &xLen, x) != EOF){
	dist[0][0] = 0;
	int i;
	for (i = 1; i <= xLen; i++)
		dist[i][0] = i;
	int j;
	for (j = 1; j <= yLen; j++)
		dist[0][j] = j;

	int deletions, insertions, changes;
	for (i = 1; i <= xLen; i++)
		for (j = 1;j <= yLen; j++){
			deletions = dist[i][j - 1] + 1;
			insertions = dist[i - 1][j] + 1;
			changes = x[i - 1] == y[j - 1] ? dist[i - 1][j - 1] : dist[i - 1][j - 1] + 1;
			dist[i][j] = MIN( MIN(deletions, insertions), changes);
		}

	printf("%d\n", dist[xLen][yLen]);
	}

	return 0;
}

你可能感兴趣的:(动态规划,poj,最小编辑距离,3356,AGTC)