Created
January 12, 2012 05:16
-
-
Save capcase/1598920 to your computer and use it in GitHub Desktop.
words transform approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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