add random stuffs

This commit is contained in:
tema5002
2026-05-24 20:13:56 +03:00
parent 7c792bf4b2
commit a8e03c5e3b
14 changed files with 931 additions and 1 deletions

41
levenshtein.h Normal file
View File

@@ -0,0 +1,41 @@
#pragma once
#include <stdlib.h>
#include <string.h>
#ifndef min
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef min3
#define min3(a, b, c) (min(min(a, b), c))
#endif
static inline int levenshtein(const char* s1, const char* s2) {
int len1 = strlen(s1);
int len2 = strlen(s2);
int* row = malloc((len2 + 1) * sizeof(int));
if (!row) return -1;
for (int j = 0; j <= len2; j++)
row[j] = j;
for (int i = 1; i <= len1; i++) {
int prev_diag = row[0];
row[0] = i;
for (int j = 1; j <= len2; j++) {
int temp = row[j];
row[j] = min3(
row[j] + 1, // deletion
row[j-1] + 1, // insertion
prev_diag + (s1[i-1] != s2[j-1]) // substitution
);
prev_diag = temp;
}
}
int result = row[len2];
free(row);
return result;
}