[算法] poj 3356 字符串的距离 AGTC

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int dp[N][N];
char a[N], b[N];
int n, m;
int get_min(int x, int y, int z) {
	return min(min(x, y), z);
}
int main() {
	while(cin >> n) {
		scanf("%s", a+1);
		scanf("%d%s", &m, b+1);
		for(int i = 0; i <= n; i++) {
			dp[i][0] = i;
		}
		for(int i = 0; i <= m; i++) {
			dp[0][i] = i;
		}
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= m; j++) {
				if(a[i] == b[j]) {
					dp[i][j] = dp[i-1][j-1];
				}
				else dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
			}
		}
		cout << dp[n][m] << endl;
	}
	return 0;
}

posted @ 2013-04-10 11:09  小尼人00  阅读(133)  评论(0编辑  收藏  举报