Advertisement
KillianMills

sneaky.cpp

Oct 1st, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include "sneaky.h"
  2.  
  3. #include <string>
  4. #include <vector>
  5. #include <iterator>
  6. #include <map>
  7. #include <iostream>
  8. #include <sstream>
  9.  
  10. using namespace std;
  11.  
  12. //sneaky::sneaky(bool db){db = debug;}
  13.  
  14. string sneaky::keyMaker(string word, char character){ // makes a unique key based on the occurences of letters
  15.     string key;
  16.    
  17.     for(int i= 0; i < word.length(); i++){ // goes through the whole word
  18.         if(word[i]== character){ // if the letter is in the word
  19.             stringstream ss;
  20.             ss << i;
  21.             key = key + "-" + ss.str(); // adds a dash after every number is added to the key
  22.         }
  23.     }
  24.     return key;
  25. }
  26.  
  27. map<string, vector<string> > sneaky::mapMaker(vector<string>& library , char character){ // makes map holding all words sorted by letter position
  28.  
  29.     map <string, vector<string> > myMap; // a map of vectors with a string as the key
  30.    
  31.     for(int k = 0; k < library.size(); k++){ // while original library is not empty
  32.         string tempKey = ""; //for making the unique key for each word
  33.         temp = library[k]; // temporarly hold word being checked in a string
  34.    
  35.         tempKey = keyMaker(temp, character);
  36.        
  37.         if(myMap.count(tempKey)== 1){ // key is found, push it into the existing key
  38.             myMap[tempKey].push_back(temp);
  39.         }  
  40.        
  41.         else // key is not found, must insert with the new key
  42.         {
  43.             vector<string> tempVector;
  44.             tempVector.push_back(temp);
  45.             myMap[tempKey] = tempVector; // add it to the map vector at the index it occurred
  46.         }
  47.     }
  48.    
  49.     return myMap;
  50. }
  51.  
  52. void debuger(int fSize , bool d){
  53.     if(d==true){
  54.         cout << "\n" << "PICKED LARGEST FAMILY OF SIZE " << fSize << "\n\n" ;
  55.     }
  56. }
  57.  
  58. void sneaky::makeFamily(vector<string>& library, char character, bool debug ){ // stores families of vectors
  59.  
  60.     bool db = debug;
  61.  
  62.     map<string, vector<string> > newMap = mapMaker(library, character); // makes map holding all words sorted by letter position
  63.    
  64.     map<string, vector<string> >::iterator checker;
  65.     vector<string> biggest; // will hold biggest family
  66.     int largest = 0; // largest familiy set to 0 at first
  67.     string largestKey = ""; // largest key set to null string at first
  68.    
  69.    
  70.     for(checker = newMap.begin(); checker!=newMap.end(); checker++){ //need to c and chose the biggest vector within the map to use
  71.         string tempKey = checker->first;
  72.         if(newMap[tempKey].size() > largest){ // if new vector is bigger
  73.             largest = newMap[tempKey].size(); // set largest number to the size of the vector we are currently at
  74.             largestKey = tempKey; // set the largest key to the key we are currently at
  75.         }
  76.     }
  77.    
  78.     debuger(largest, db); // shows how familes are being chosen in debug mode
  79.    
  80.     biggest = newMap[largestKey];// sets the vector biggest to the biggest family we found
  81.    
  82.     library.clear();
  83.    
  84.     for(int i=0; i < biggest.size(); i++){
  85.         library.push_back(biggest[i]);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement