1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
int stringDiffer( const string &x, const string &y )
{
int difference;
if ( x.empty() ) difference= y.length();
else if ( y.empty() ) difference= x.length();
else difference= ( x[0] != y[0] ) + stringDiffer( x.substr( 1 ), y.substr( 1 ) );
return difference ;
}
int distanceFinder(const std::vector<string> &v, int n)
{
auto i1 = std::adjacent_find(v.begin(), v.end(), [=](string x,string y)
{
int difference;
difference= stringDiffer( x, y );
return (difference<=n);
}
);
int returnValue;
if (i1 == v.end()) {
returnValue=-1;
} else {
returnValue= std::distance(v.begin(), i1) ;
}
return returnValue;
}
int main() {
const std::vector<std::string> more_words {
"this is a sentence.",
"this is also words ",
"another line in one",
"another LINE_in one",
"stuff words in open",
"stuff words in OPEN",
"whitespace\n\tagain ",
};
std::cout<<distanceFinder(more_words, 5)<<std::endl;
std::cout<<distanceFinder(more_words, 4)<<std::endl;
std::cout<<distanceFinder(more_words, 0)<<std::endl;
//std::cout<<distanceFinder(more_words, 0)<<std::endl;
return 0;
}
| |