Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@capcase
Created January 12, 2012 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capcase/1598920 to your computer and use it in GitHub Desktop.
Save capcase/1598920 to your computer and use it in GitHub Desktop.
words transform approach
int string_compare(char *s, char *t, int i, int j, )
// s is inut text
// t is target
{
int k; /* counter */
int opt[3]; /* cost of the three options */
int lowest_cost; /* lowest cost */
if (i == 0) return(j * cost.insertion);
if (j == 0) return(i * cost.deletion);
if (i==1 && j==1 && s[i] == t[j]) {
return 0;
}
if (s[i] == t[j]) {
return string_compare(s,t,i-1,j-1);
} else {
cost1 = cost2 = cost3 = INT.MAX
// replace a character
if ( s[:i-1].append(t[j]) in dictionary) {
cost1 = string_compare(s,t,i-1,j-1) + cost.replace
}
// extra character in input. delete one
if (s[:i-1] in dictionary) {
cost2 = string_compare(s,t,i-1,j) + cost.deletion
}
// extra character in target. insert one
if (s[:i].append(t[j]) in dictionary) {
cost3 = string_compare(s,t,i,j-1) + cost.insertion
}
lowest_cost = min ( cost1, cost2, cost3)
}
return( lowest_cost );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment