Advertisement
Guest User

Untitled

a guest
Feb 13th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <map>
  4. #include <set>
  5.  
  6. #include "wordlist.hpp"
  7.  
  8. using namespace std;
  9.  
  10. map<char, set<string> > PrepList(std::string list[], size_t size){
  11.     map<char, set<string> > containsList;
  12.     for(int i=0; i<size; ++i){
  13.         for(string::iterator j=list[i].begin(); j!=list[i].end(); ++j){
  14.             containsList[*j].insert(list[i]);
  15.         }
  16.     }
  17.     return containsList;
  18. }
  19.  
  20. int countLetter(char letter, string word){
  21.     int ret=0;
  22.     for(string::iterator i=word.begin(); i!=word.end(); ++i){
  23.         if(*i==letter) ret++;
  24.     }
  25.     return ret;
  26. }
  27.  
  28. set<string> GetAnagrams(string word, map<char, set<string> > containsList){
  29.     //add all words containing first letter to "possibles" list
  30.     set<string> possibles=containsList[word[0]];
  31.     //for each letter in the word, remove any possiblities that do not contain this letter
  32.     for(string::iterator i=word.begin(); i!=word.end(); ++i){
  33.         set<string> remove;
  34.         for(set<string>::iterator j=possibles.begin(); j!=possibles.end(); ++j){
  35.             if(j->length()!=word.length() || containsList[*i].count(*j)!=1){
  36.                 remove.insert(*j);
  37.             }
  38.         }
  39.         for(set<string>::iterator j=remove.begin(); j!=remove.end(); ++j){
  40. #ifdef DEBUG_OUTPUT
  41.             cout << "Possibility '" << *j << "' removed. ";
  42.             if(j->length()!=word.length()){
  43.                 cout << "(wrong length)";
  44.             }else{
  45.                 cout << "(does not contain '" << *i << "')";
  46.             }
  47.             cout << endl;
  48. #endif
  49.             possibles.erase(*j);
  50.         }
  51.     }
  52.     //for each letter in the word, check that the possibilities contain the same number of
  53.     //instances of that letter
  54.     for(string::iterator i=word.begin(); i!=word.end(); ++i){
  55.         set<string> remove;
  56.         for(set<string>::iterator j=possibles.begin(); j!=possibles.end(); ++j){
  57.             if(countLetter(*i, word)!=countLetter(*i, *j)){
  58.                 remove.insert(*j);
  59.             }
  60.         }
  61.         for(set<string>::iterator j=remove.begin(); j!=remove.end(); ++j){
  62. #ifdef DEBUG_OUTPUT
  63.             cout << "Possibility '" << *j << "' removed. (wrong number of '" << *i << "'s)" << endl;
  64. #endif
  65.             possibles.erase(*j);
  66.         }
  67.     }
  68.     return possibles;
  69. }
  70.  
  71. int main(){
  72.     map<char, set<string> > containsList=PrepList(wordlist, wordlist_size);
  73.     string word="NOTBLANK";
  74.     while(word != ""){
  75.         cout << "Enter word (blank to quit):";
  76.         getline(cin, word);
  77.         cout << "Possibilities:" << endl;
  78.         set<string> anagrams=GetAnagrams(word, containsList);
  79.         for(set<string>::iterator i=anagrams.begin(); i!=anagrams.end(); ++i){
  80.             cout << *i << endl;
  81.         }
  82.     }
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement