1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 7 int main() 8 { 9 char strA[10] = "abddd"; 10 char strB[10] = "aebdd"; 11 int lenA, lenB; 12 lenA=strlen(strA); 13 lenB=strlen(strB); 14 15 //dp 16 int dp[lenA+1][lenB+1]; 17 for(int i=0; i<=lenA; i++) 18 { 19 for(int j=0; j<=lenB; j++) 20 { 21 dp[i][j] = lenA+lenB+1; 22 } 23 } 24 dp[0][0] = 0; 25 for(int i=0; i<lenA; i++) 26 { 27 for(int j=0; j<lenB; j++) 28 { 29 if(strA[i]==strB[j]) 30 { 31 dp[i+1][j+1] = min(dp[i][j],min(dp[i+1][j],dp[i][j+1])+1); 32 } 33 else 34 { 35 dp[i+1][j+1] = min(dp[i][j],min(dp[i+1][j],dp[i][j+1]))+1; 36 } 37 } 38 } 39 cout<<dp[lenA][lenB]<<endl; 40 return 0; 41 }
![](https://img2024.cnblogs.com/blog/35695/202407/35695-20240713070336838-1837943664.jpg)