Skip to content

Instantly share code, notes, and snippets.

@acoster
Created October 7, 2013 22:07
Show Gist options
  • Save acoster/6875760 to your computer and use it in GitHub Desktop.
Save acoster/6875760 to your computer and use it in GitHub Desktop.
Getting all anagrams of the english language. The filtered_dict file was generated with: curl "http://www.freebsd.org/cgi/cvsweb.cgi/src/share/dict/web2?rev=1.12;content-type=text%2Fplain" | tr "[:upper:]" "[:lower:]" | uniq > filtered_dict
#include <map>
#include <string>
#include <fstream>
#include <iostream>
int main(int argc, const char * argv[])
{
std::multimap<std::string, std::string> contents;
std::ifstream inFile("filtered_dict");
char szWord[128];
inFile.getline(szWord, sizeof(szWord));
while (!inFile.eof())
{
std::string key(szWord);
std::string word(szWord);
std::sort(key.begin(), key.end());
contents.insert(std::make_pair(key, word));
inFile.getline(szWord, sizeof(szWord));
}
auto it(std::begin(contents));
for ( ; it != std::end(contents); ++it)
{
auto range(contents.equal_range(it->first));
if (std::distance(range.first, range.second) == 1)
continue;
std::for_each(range.first, range.second, [&](const std::pair<std::string, std::string>&p) {std::cout << p.second << " ";});
std::cout << std::endl;
it = range.second;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment