anagram: naive version; almost works, lowercase conversion is needed(?)

This commit is contained in:
Dmitry Kokorin 2016-02-25 17:43:12 +03:00
parent ee13c07c31
commit a4e6b33366
2 changed files with 84 additions and 0 deletions

59
cpp/anagram/anagram.cpp Normal file
View 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;
}
}