编辑距离
51Nod - 1183补一道经典dp问题~
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1010; 4 5 char s[maxn], p[maxn]; 6 int dp[maxn][maxn]; 7 8 int main(){ 9 scanf("%s %s", s, p); 10 int n = strlen(s); 11 int m = strlen(p); 12 for(int i = 0; i <= n; i++) dp[i][0] = i; 13 for(int i = 0; i <= m; i++) dp[0][i] = i; 14 for(int i = 1; i <= n; i++){ 15 for(int j = 1; j <= m; j++){ 16 dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1; 17 dp[i][j] = min(dp[i][j], dp[i-1][j-1] + (s[i-1] != p[j-1])); 18 } 19 } 20 printf("%d ", dp[n][m]); 21 }