题解 | #计算字符串的距离#
计算字符串的距离
http://www.nowcoder.com/practice/3959837097c7413a961a135d7104c314
#include<iostream>
#include<vector>
using namespace std;
int main()
{
string word1,word2;
while(cin>>word1>>word2)
{
int m=word1.size();
int n=word2.size();
vector<vector<int>>dp(m+1,vector<int>(n+1,0));
for(int i=0;i<m+1;i++)dp[i][0]=i;
for(int j=0;j<n+1;j++)dp[0][j]=j;
for(int i=1;i<m+1;i++)
{
for(int j=1;j<n+1;j++)
{
int v=0;
if(word1[i-1]!=word2[j-1])
{
v=1;
}
dp[i][j]=min(min(dp[i-1][j]+1,dp[i][j-1]+1),dp[i-1][j-1]+v);
}
}
cout<<dp[m][n]<<endl;
}
return 0;
}