Last active
December 18, 2015 23:39
-
-
Save ygabo/5863213 to your computer and use it in GitHub Desktop.
Get all the anagrams of a word. (Given a dictionary)
This file contains 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 <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