Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <set>
- #include <iostream>
- #include <string>
- #include "wordlist.hpp"
- using namespace std;
- int Hash(string word){
- int ret=0;
- for(string::iterator i=word.begin(); i!=word.end(); ++i){
- ret+=*i;
- }
- return ret;
- }
- map<int, set<string> > HashList(string list[], size_t size){
- map<int, set<string> > ret;
- for(int i=0; i<size; ++i){
- ret[Hash(list[i])].insert(list[i]);
- }
- return ret;
- }
- int CountLetter(char letter, string word){
- int ret=0;
- for(string::iterator i=word.begin(); i!=word.end(); ++i){
- if(*i==letter) ret++;
- }
- return ret;
- }
- bool IsMatch(string a, string b){
- if(a.length()!=b.length()) return false;
- for(string::iterator i=a.begin(); i!=a.end(); ++i){
- if(CountLetter(*i, a)!=CountLetter(*i, b)) return false;
- }
- return true;
- }
- set<string> GetMatches(string word, map<int, set<string> > hashlist){
- set<string> matches=hashlist[Hash(word)];
- set<string> ret;
- for(set<string>::iterator i=matches.begin(); i!=matches.end(); ++i){
- if(IsMatch(word, *i)) ret.insert(*i);
- }
- return ret;
- }
- int main(){
- map<int, set<string> > hashlist=HashList(wordlist, wordlist_size);
- string word="NOTBLANK";
- while(word != ""){
- cout << "Enter word (blank to quit):";
- getline(cin, word);
- cout << "Matches:" << endl;
- set<string> matches=GetMatches(word, hashlist);
- for(set<string>::iterator i=matches.begin(); i!=matches.end(); ++i){
- cout << *i << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement