Created
February 11, 2016 02:44
-
-
Save audioplastic/79d0c0b83e16e36c81d3 to your computer and use it in GitHub Desktop.
Breakdown statistics of all of the anagrams in a language file using alphabetised hashing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <algorithm> | |
#include <map> | |
int main(int argc, char *argv[]) | |
{ | |
std::ifstream file("words.txt"); | |
std::multimap<std::string, std::string> alphamap; | |
std::string word; | |
// Hash everything with alphabetized key | |
while (file >> word) { | |
auto wKey = word; | |
std::sort(wKey.begin(), wKey.end()); | |
alphamap.insert({wKey, word}); | |
} | |
std::cout << "Word count in dictionary:" << alphamap.size() << std::endl; | |
// Hash the alphabetized keys with score key | |
std::multimap<size_t, std::string> scoremap; | |
for( auto it = alphamap.begin(), end = alphamap.end(); | |
it != end; | |
it = alphamap.upper_bound(it->first) ) { | |
const auto score = alphamap.count(it->first); | |
scoremap.insert({score, it->first}); | |
} | |
//Display | |
for( auto it = scoremap.begin(), end = scoremap.end(); | |
it != end; | |
it = scoremap.upper_bound(it->first)){ | |
const auto score = it->first; | |
const auto groupcount = scoremap.count(it->first); | |
std::cout << "Words with " << score << " anagrams have " << groupcount << " distinct groups." << std::endl; | |
if (score > 11) { | |
for (auto group_it = scoremap.lower_bound(score); group_it != scoremap.upper_bound(score); ++group_it) { | |
const auto grpKey = group_it->second; | |
std::cout << "[" << grpKey << "]: "; | |
for (auto member_it = alphamap.lower_bound(grpKey); member_it != alphamap.upper_bound(grpKey); ++member_it) { | |
std::cout << member_it->second << ", "; | |
} | |
std::cout << std::endl; | |
} | |
std::cout << std::endl; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment