1

I 'm reading "The Algorithm Design Manual (2nd Edition)". C++ is new for me. I try to use example of author: string_compare(), and only code by myself main(). Output is wrong. I guess my main 's having problem with char s[], pointer. Anyone can help me finding my mistake.

Code by C++, and very simple input

int main()
{
    char s[] = "A"; // "thou shalt not"; //"FOOD";
    char t[] = "B"; // "you should not"; //"MONEY";
    int i = sizeof(s)/sizeof(char);
    int j = sizeof(t)/sizeof(char);
    int resultDistance = string_compare(s, t, i, j);
    printf("N steps = %d\n", resultDistance);
    reconstruct_path(s, t, i, j);
}

int string_compare(char *s, char *t, int i, int j)
{
    int k;          /* counter */
    int opt[3];     /* cost of the three options */
    int lowest_cost;    /* lowest cost */

    if (i == 0) return(j * indel(' '));
    if (j == 0) return(i * indel(' '));

    opt[MATCH] = string_compare(s,t,i-1,j-1) + match(s[i],t[j]);
    opt[INSERT] = string_compare(s,t,i,j-1) + indel(t[j]);
    opt[DELETE] = string_compare(s,t,i-1,j) + indel(s[i]);

    lowest_cost = opt[MATCH];
    for (k=INSERT; k<=DELETE; k++)
        if (opt[k] < lowest_cost) lowest_cost = opt[k];

    m[i][j].cost = lowest_cost; /* REMOVE FROM PRINTED VERSION */

    return( lowest_cost );
} 

int reconstruct_path(char *s, char *t, int i, int j)
{
    /*printf("trace (%d,%d)\n",i,j);*/

    if (m[i][j].parent == -1) return(0);

    if (m[i][j].parent == MATCH) {
        reconstruct_path(s,t,i-1,j-1);
        match_out(s, t, i, j);
        return(0);
    }
    if (m[i][j].parent == INSERT) {
        reconstruct_path(s,t,i,j-1);
        insert_out(t,j);
        return(0);
    }
    if (m[i][j].parent == DELETE) {
        reconstruct_path(s,t,i-1,j);
        delete_out(s,i);
        return(0);
    }
}

int match_out(char *s, char *t, int i, int j)
{
    if (s[i]==t[j]) printf("M");
    else printf("S");
    return(0);
}

void insert_out(char *t, int j)
{
    printf("I");
}

void delete_out(char *s, int i)
{
    printf("D");
}

int indel(char c)
{
    return(1);
}

int match(char c, char d)
{
    if (c == d) return(0);
    else return(1);
}

My code on github: https://github.com/hoangvu1991/EditDistanceRecursive/blob/master/EditDistanceRecursive.cpp

actual: 0 | expect:1

  • This code does not compile. [mcve] would be helpful. – Eljay Jul 13 '19 at 15:30
  • `char s[] = "A";` must be `const char s[] = "A";` or `const auto s = "A";`. Also change `int string_compare(char *s, char *t, int i, int j)` to `int string_compare(const char *s, const char *t, int i, int j)` – Jesper Juhl Jul 13 '19 at 15:35
  • @Jesper Juh: Thanks, I understood yourd advices will make my code become better. but is it solved my problem ? –  Jul 14 '19 at 02:40
  • @user1879108 First of all, why don't you just try? Secondly, `"A"` has type `const char[2]` which you cannot assign to a non-const variable. So changing that fixes at least part of your problem. – Jesper Juhl Jul 14 '19 at 05:37

1 Answers1

1

Try following:

opt[MATCH] = string_compare(s,t,i-1,j-1) + match(s[i-1],t[j-1]);

instead of

opt[MATCH] = string_compare(s,t,i-1,j-1) + match(s[i],t[j]);
nav_jan
  • 2,473
  • 4
  • 24
  • 42
  • your suggestion seem ok, but I 'm assume book 's example is not wrong, I 'm refered errata for this book I guess my main() (my code is in main() only), and my thinking having problem –  Jul 13 '19 at 16:01