两个字符串可以经过修改,增加, 删除一个字符作为一个操作,经过n步操作,两个字符串变为一样的,
相似度就为n,求n。
1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 char strA[10] = "abddd"; 7 char strB[10] = "aebdd"; 8 9 int calcDistance(int sa, int ea, int sb, int eb) 10 { 11 if(sa > ea) 12 { 13 if(sb > eb) 14 { 15 return 0; 16 } 17 else 18 { 19 return eb - sb + 1; 20 } 21 } 22 if(sb > eb) 23 { 24 if(sa > ea) 25 { 26 return 0; 27 } 28 else 29 { 30 return ea - sa + 1; 31 } 32 } 33 34 if(strA[sa] == strB[sb]) 35 { 36 return calcDistance(sa+1, ea, sb+1, eb); 37 } 38 else 39 { 40 int d1=calcDistance(sa+1, ea, sb, eb); 41 int d2=calcDistance(sa, ea, sb+1, eb); 42 int d3=calcDistance(sa+1, ea, sb+1, eb); 43 return min(d1,min(d2,d3))+1; 44 } 45 } 46 47 int main() 48 { 49 cout<<calcDistance(0,strlen(strA)-1,0,strlen(strB)-1)<<endl; 50 return 0; 51 }