Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Substring search
//Title of this code #include <iostream> using namespace std; /* hellobello valid: lb - one missing o valid: elo - one missing l valid: loob - one more plus o abcdefghij cdeghij */ int search(const string& str, const string& pattern, int maxError) { for (int i = 0; i < str.length(); ++i) { int error = 0; int missing = 0; int plus = 0; for (int j = 0; j < pattern.length(); ++j) { if(i + j >= str.length()) { ++error; } else if ((str[i + j] != pattern[j]) && (str[i + j] != pattern[j - missing]) && (str[i + j] != pattern[j + plus])) { ++error; // pattern missing hellobello - elob (missing L) ++missing; ++plus; // pattern plus } if (error > maxError) break; cout << str[i + j] << "-" << pattern[j] << " "; } cout << endl; if (error <= maxError) return i + 1; } return -1; } int main() { //cout << search("hellobello", "elobell", 1); cout << search("hellobello", "helnnlobe", 2); //cout << endl; //cout << search("abcdefghij", "cdeghij", 1); }
run
|
edit
|
history
|
help
0
Baaad1
DailyExchRate2
LIS
Dar
HotelVec
Tower of hanoi
code_chef
ExtremeValues
Value equal to index value
ListTel