poj3356
与最长公共子序列相似,dp.
注意循环的范围,不要总是想都不想就用小于。这题就应该用小于等于。
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; #define maxn 1001 int len1, len2; string st1, st2; int f[maxn][maxn]; int main() { //freopen("D:\\t.txt", "r", stdin); while (cin >> len1 >> st1 >> len2 >> st2) { memset(f, 0, sizeof(f)); for (int i = 0; i <= len1; i++) f[i][0] = i; for (int i = 0; i <= len2; i++) f[0][i] = i; for (int i = 1; i <= len1; i++) for (int j = 1; j <= len2; j++) f[i][j] = min(min(f[i - 1][j] + 1, f[i][j - 1] + 1), f[i - 1][j - 1] + (st1[i - 1] != st2[j - 1])); cout << f[len1][len2] << endl; } return 0; }