Closed Yancey2023 closed 2 weeks ago
If you have the time could you write tests for this algorithm?
Hi, I've added some test cases! Additionally, I've changed the return value to the starting index of the pattern. Perhaps the usage of std::string::npos
and size_t
in the code might confuse beginners, but this is based on my reference to std::string::find()
, which indeed returns a size_t
type and returns std::string::npos
when the substring is not found.
I don't understand:
#ifdef _MSC_VER
#include <string> // use this for MS Visual C++
#else
#include <cstring>
#endif
I don't understand:
#ifdef _MSC_VER #include <string> // use this for MS Visual C++ #else #include <cstring> #endif
Why not just use #include <string>
?
It's old code feel free to remove
Thanks. I have deleted it.
Description of Change
For
strings/knuth_morris_pratt.cpp
:fix: if
pattern
is empty, it should immediately return, otherwisepattern[k]
will access out of bounds.feat: let the KMP algorithm return index and add more tests.
Checklist
Notes: