1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
int min(int a,int b,int c)
{
if(a<b && a<c)
return a;
else if(b<a && b<c)
return b;
else
return c;
}
int editdist(string str,string sub)
{
int row,col;
row=1;
col=1;
while(row<=str.size())
{
while(col<=sub.size())
{
if(str[row-1]==sub[col-1]) {
edit[row][col]=edit[row-1][col-1];
} else {
cout << "DEBUG:" << str[row-1] << "!=" << sub[col-1] << endl;
// print anything else which interests you
edit[row][col]=min(edit[row][col-1],edit[row-1][col-1],edit[row-1][col])+1;
}
col++;
}
row++;
col=1;
}
}
int main ( ) {
// test your min function
cout << min(1,2,3) << endl;
cout << min(2,1,3) << endl;
cout << min(3,2,1) << endl;
// test your editdist function
string str = "ABC", sub = "BCD";
editdist(str,sub);
cout<<edit[str.size()][sub.size()]<<endl;
}
|