diff --git a/libpz/fuzzy/ukkonen.cpp b/libpz/fuzzy/ukkonen.cpp new file mode 100644 index 0000000..134c002 --- /dev/null +++ b/libpz/fuzzy/ukkonen.cpp @@ -0,0 +1,39 @@ +// Ukkonen Cutoff DP +#include +#include +#include +#include + +using namespace std; + +bool edit_within_k(const string& s, const string& t, int k) { + if (k < 0) return false; + int m = s.size(); + int n = t.size(); + if(abs(m-n) > k) return false; + int INF= k + 1; + vector prev(2*k + 3, INF); + vector curr(2*k + 3, INF); + + for(int j = 0; j <= min(n, k); j++) { + int d=j; + prev[d+k+1] = j; + } + for(int i= 1; i <= m; i++) { + curr[0] = i; + int left = max(1, i-k); + int right = min(n, i+k); + for (int j=left; j<= right; j++) { + int d = j-i; + int idx = d+k+1; + int cost = (s[i-1] == t[j-1]) ? 0 : 1; + curr[idx] = min({ + prev[idx+1] + 1, + curr[idx-1] + 1, + prev[idx] + cost + }); + } + swap(prev, curr); + } + return prev[n-m+k+1] <= k; +} \ No newline at end of file diff --git a/libpz/include/ukkonen.hpp b/libpz/include/ukkonen.hpp new file mode 100644 index 0000000..aec5cac --- /dev/null +++ b/libpz/include/ukkonen.hpp @@ -0,0 +1,15 @@ +#ifndef UKKONEN_HPP +#define UKKONEN_HPP +#include +#include +#include + +class Ukkonen { +public: + explicit Ukkonen(int k); + bool edit_within_k(const std::string& s, const std::string& t); + +private: + int k_; +}; +#endif \ No newline at end of file