Note that there are some explanatory texts on larger screens.

plurals
  1. POLevenshtein distance in C with required memory of O(m)
    primarykey
    data
    text
    <p>I'm writing a code that calculates the edit distance of two given strings t and s with m = strlen(t) and n = strlen(s) and the code should only use memory in O(m). Furthermore, it should not need longer than 4 seconds for the calculation of two strings with about 50 characters. My written code satisfies the latter, but I'm not sure about the first, so I'd be happy if you could check if it uses not more than O(m) memory. If it doesn't, it would also be useful to hear some tips how to do so. Thanks.</p> <pre><code>#include &lt;stdio.h&gt; /************************************/ /* TODO: Insert possible help */ /* functions in here */ /************************************/ int min (int a[], int l) { int i, min = a[0]; for (i = 1; i &lt; l; i++) { if (a[i] &lt; min) { min = a[i]; } } return min; } int main() { /* n, m, s, t are configurable in the source, i.e. the values here are only an example and shall be changed to any value.*/ const int n = 58; /* length of s, &gt; 0 */ const int m = 54; /* length of t, &gt; 0 */ char *s = "Calculateanddisplaythenumberofcharacterswithinarewrwegurie"; char *t = "Simplycopyeverythinginsideandpasteitwhereogwrgheigrber"; /* Save the edit distance in res */ int res; int matrix[n+1][m+1]; int i, j; for (i = 0; i &lt;= m; i++) { matrix[n][i] = m-i; } for (i = 0; i &lt;= n; i++) { matrix[i][m] = n-i; } for (i = n-1; i &gt;= 0; i--) { for (j = m-1; j &gt;= 0; j--) { int cost = (s[i] != t[j]); int b[3]; b[0] = 1+matrix[i][j+1]; b[1] = 1+matrix[i+1][j]; b[2] = matrix[i+1][j+1]+cost; matrix[i][j] = min(b, 3); } } res = matrix[0][0]; /* Output */ printf("The edit distance is %d.\n\n", res); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload