Avatar of jaxrpc
jaxrpc
 asked on

Anagrams C++

Hi,
i would like to group words together...for example.
i have 3 words in my dictionary
cater
trace
acert

i would like to for all the words in my dictionary pair them maybe using map

e.g
acert - cater
acert - trace

cater - acert
cater - trace

trace - cater
trace - acert

kinda loss....i am expecting my dictionary to have 30k+ words. How can i do it efficiently also. Thanks just some pointers and directions.
C++

Avatar of undefined
Last Comment
itsmeandnobodyelse

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jkr

jaxrpc

ASKER
hi, thanks for direction....but after generating the possible permutation...i would loop through the my 30k words in the dictionary to see if the permutation is in the dictionary and then add it to my key, value map.
but if i would to loop through 30k for each permutation, it will be slow i think...are there any better ways of solving this?
jkr

Just add it to the map. A map will not accept any duplicates anyway.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
eleven_squared

you could use a multimap with key = sorted word, value = word.
to lookup, sort the word and use as key. eg.

#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std ;

int main()
{
  multimap<string,string> lookup ;
  ifstream file( "dictionary.txt" ) ;
  string str ;
  while( file >> str )
  {
    string key = str ;
    sort( key.begin(), key.end() ) ;
    lookup.insert( pair<string,string>(key,str) ) ;
  }

  {
    string what ;
    cout << "enter word: " ; cin >> what ;
    sort( what.begin(), what.end() ) ;
    typedef multimap<string,string>::iterator iterator ;
    pair<iterator,iterator> bounds = lookup.equal_range(what) ;
    for( iterator iter = bounds.first ; iter != bounds.second ; ++iter )
      cout << iter->second << '\n' ;
  }

}
jaxrpc

ASKER
hi eleven_squared,
would you mind explaning while(file >> str) str is = "" how does file know how many bytes to read from the file so that for that n bytes is = to 1 word? what if the dictionary is newline delimited?

thks
eleven_squared

str is a std::string; when reading from the file the string resizes itself to accommodate all chars read till a white space is encountered.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
eleven_squared

the word in the dictionay could be delimited by any white space (space, tab, newline etc.)
jaxrpc

ASKER
hi, thanks for the tips... i am finding out why some of the things done that way.....
guess i really need to do practise more....i have been learning for 1 month + but i took so long to think of how to do something which you can do within minutes.....
itsmeandnobodyelse

I wouldn't use a multimap (it has really a ugly and uncomfortable interface). Better use a

   map<string, vector<string> >   permMap;

where the key is the sorted permutation key (unique) and the vector contains all occurences. Read the dictionary file like eleven_sqared had showed (the strings either should be separated by space or by newline), make the sorted key and add it to the map by

        permMap[key].push_back(str);

if the key doesn't exist a vector was created. If it exists you'll get a reference to the existing vector. To get the output required you need to iterate the map and iterate each vector inside of the loop as well.

Regards, Alex

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
eleven_squared

jaxrpc, do not loose heart. it takes time before programming starts looking really easy. everyone would have been in your position when they started out; i certainly was!
jaxrpc

ASKER
itsmeandnobodyelse, i will try out your suggestion too...
eleven squared : looks like the STL is really useful
eleven_squared

yes, stl is really useful. you would find a lot of ways to do things in a much easier way once you are familiar with it.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jaxrpc

ASKER
i was looking at cppreference.com i am trying to find some info on the pair template...but i found nothing...which area is it under?
itsmeandnobodyelse

 
  while( ifs >> str )  // will stop at end of file or error
  {
    string key = str ;
    sort( key.begin(), key.end() ) ;
    permMap[key].push_back(str);
  }
  ifs.close();
  ofstream ofs("perm.txt");
  map<string, vector<string> >::iterator it;
  for (it = permMap.begin(); it != permMap.end(); ++it)
  {
         vector<string>& v = it->second;
         vector<string>::iterator vit1;
         for (vit1 = v.begin(); vit1 != v.end(); ++vit1)
         while (vit1 != v.end())
         {
              vector<string>::iterator vit2;
               for (vit2 = v.begin(); vit2 != v.end(); ++vit2)
               {
                   if (vit1 == vit2)
                         continue;
                  ofs << *vit1 << " - " << *vit2 << endl;
               }
           }
     }
     // close file
    ...

jkr

Don't know that site, but you can find more information on http://www.sgi.com/tech/stl/pair.html and http://www.sgi.com/tech/stl/PairAssociativeContainer.html
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
eleven_squared

here is one reference: http://www.sgi.com/tech/stl/pair.html
itsmeandnobodyelse

>>>> to find some info on the pair template...but i found nothing..
class pair is defined in <utility>. Maybe there is a topic in cppreference. You should get it via map as well.
itsmeandnobodyelse

>>>>         while (vit1 != v.end())
That line must be removed. I replaced it by the for loop above.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.