anagram: naive version; almost works, lowercase conversion is needed(?)
This commit is contained in:
parent
ee13c07c31
commit
a4e6b33366
2 changed files with 84 additions and 0 deletions
59
cpp/anagram/anagram.cpp
Normal file
59
cpp/anagram/anagram.cpp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#include "anagram.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <locale>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace anagram {
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
static inline string normalize(const string &str)
|
||||
{
|
||||
string result = str;
|
||||
|
||||
transform(result.begin(), result.end(),
|
||||
result.begin(),
|
||||
[](char c){return tolower(c);});
|
||||
|
||||
sort(result.begin(), result.end());
|
||||
|
||||
cout << str << " -> " << result << endl;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
anagram::anagram(const string &_key)
|
||||
{
|
||||
cout << "New test, key=" << _key << endl;
|
||||
key = normalize(_key);
|
||||
}
|
||||
|
||||
StringContainer
|
||||
anagram::matches(const StringContainer &input) const
|
||||
{
|
||||
StringContainer result(input.size());
|
||||
|
||||
cout << "Input: {";
|
||||
for (auto s : input)
|
||||
cout << s << " ";
|
||||
cout << "}" << endl;
|
||||
|
||||
auto iter = copy_if(input.cbegin(), input.cend(),
|
||||
result.begin(),
|
||||
[=](const string &s){return key == normalize(s);});
|
||||
|
||||
cout << "Output: {";
|
||||
for (auto s : result)
|
||||
cout << s << " ";
|
||||
cout << "}" << endl;
|
||||
|
||||
result.resize(distance(result.begin(), iter));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue