Skip to content

Instantly share code, notes, and snippets.

@ygabo
Last active December 18, 2015 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ygabo/5863213 to your computer and use it in GitHub Desktop.
Save ygabo/5863213 to your computer and use it in GitHub Desktop.
Get all the anagrams of a word. (Given a dictionary)
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
int main(){
cin.sync_with_stdio(false); // read file in chunks
string s, sorted;
stringstream ss;
// given dictionary
ifstream f("C:/Users/tehee/Desktop/cplusplus/dictionary.text");
map<string, set<string>> dict;
// read the file
while(getline(f, s)){
ss.str("");
ss << s;
ss >> s;
for(int i=0; i<s.size();++i)
s[i] = tolower(s[i]);
sorted = s;
sort(sorted.begin(), sorted.end());
dict[sorted].insert(s);
}
cout << "word: ";
while(cin >> s){
for(int i=0; i<s.size();++i)
s[i] = tolower(s[i]);
cout << s << " ";
sort(s.begin(), s.end());
cout << "key: " << s << endl;
for( auto it = dict[s].begin(); it != dict[s].end(); ++it){
cout << *it << " ";
}
cout <<endl;
cout << "word: " ;
}
cout << "done" << endl;
cin.get();
f.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment