Advertisement
Guest User

Untitled

a guest
Feb 13th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <map>
  2. #include <set>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. #include "wordlist.hpp"
  7.  
  8. using namespace std;
  9.  
  10. int Hash(string word){
  11.     int ret=0;
  12.     for(string::iterator i=word.begin(); i!=word.end(); ++i){
  13.         ret+=*i;
  14.     }
  15.     return ret;
  16. }
  17.  
  18. map<int, set<string> > HashList(string list[], size_t size){
  19.     map<int, set<string> > ret;
  20.     for(int i=0; i<size; ++i){
  21.         ret[Hash(list[i])].insert(list[i]);
  22.     }
  23.     return ret;
  24. }
  25.  
  26. int CountLetter(char letter, string word){
  27.     int ret=0;
  28.     for(string::iterator i=word.begin(); i!=word.end(); ++i){
  29.         if(*i==letter) ret++;
  30.     }
  31.     return ret;
  32. }
  33.  
  34. bool IsMatch(string a, string b){
  35.     if(a.length()!=b.length()) return false;
  36.     for(string::iterator i=a.begin(); i!=a.end(); ++i){
  37.         if(CountLetter(*i, a)!=CountLetter(*i, b)) return false;
  38.     }
  39.     return true;
  40. }
  41.  
  42. set<string> GetMatches(string word, map<int, set<string> > hashlist){
  43.     set<string> matches=hashlist[Hash(word)];
  44.     set<string> ret;
  45.     for(set<string>::iterator i=matches.begin(); i!=matches.end(); ++i){
  46.         if(IsMatch(word, *i)) ret.insert(*i);
  47.     }
  48.     return ret;
  49. }
  50.  
  51. int main(){
  52.     map<int, set<string> > hashlist=HashList(wordlist, wordlist_size);
  53.     string word="NOTBLANK";
  54.     while(word != ""){
  55.         cout << "Enter word (blank to quit):";
  56.         getline(cin, word);
  57.         cout << "Matches:" << endl;
  58.         set<string> matches=GetMatches(word, hashlist);
  59.         for(set<string>::iterator i=matches.begin(); i!=matches.end(); ++i){
  60.             cout << *i << endl;
  61.         }
  62.     }
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement