Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "sneaky.h"
- #include <string>
- #include <vector>
- #include <iterator>
- #include <map>
- #include <iostream>
- #include <sstream>
- using namespace std;
- //sneaky::sneaky(bool db){db = debug;}
- string sneaky::keyMaker(string word, char character){ // makes a unique key based on the occurences of letters
- string key;
- for(int i= 0; i < word.length(); i++){ // goes through the whole word
- if(word[i]== character){ // if the letter is in the word
- stringstream ss;
- ss << i;
- key = key + "-" + ss.str(); // adds a dash after every number is added to the key
- }
- }
- return key;
- }
- map<string, vector<string> > sneaky::mapMaker(vector<string>& library , char character){ // makes map holding all words sorted by letter position
- map <string, vector<string> > myMap; // a map of vectors with a string as the key
- for(int k = 0; k < library.size(); k++){ // while original library is not empty
- string tempKey = ""; //for making the unique key for each word
- temp = library[k]; // temporarly hold word being checked in a string
- tempKey = keyMaker(temp, character);
- if(myMap.count(tempKey)== 1){ // key is found, push it into the existing key
- myMap[tempKey].push_back(temp);
- }
- else // key is not found, must insert with the new key
- {
- vector<string> tempVector;
- tempVector.push_back(temp);
- myMap[tempKey] = tempVector; // add it to the map vector at the index it occurred
- }
- }
- return myMap;
- }
- void debuger(int fSize , bool d){
- if(d==true){
- cout << "\n" << "PICKED LARGEST FAMILY OF SIZE " << fSize << "\n\n" ;
- }
- }
- void sneaky::makeFamily(vector<string>& library, char character, bool debug ){ // stores families of vectors
- bool db = debug;
- map<string, vector<string> > newMap = mapMaker(library, character); // makes map holding all words sorted by letter position
- map<string, vector<string> >::iterator checker;
- vector<string> biggest; // will hold biggest family
- int largest = 0; // largest familiy set to 0 at first
- string largestKey = ""; // largest key set to null string at first
- for(checker = newMap.begin(); checker!=newMap.end(); checker++){ //need to c and chose the biggest vector within the map to use
- string tempKey = checker->first;
- if(newMap[tempKey].size() > largest){ // if new vector is bigger
- largest = newMap[tempKey].size(); // set largest number to the size of the vector we are currently at
- largestKey = tempKey; // set the largest key to the key we are currently at
- }
- }
- debuger(largest, db); // shows how familes are being chosen in debug mode
- biggest = newMap[largestKey];// sets the vector biggest to the biggest family we found
- library.clear();
- for(int i=0; i < biggest.size(); i++){
- library.push_back(biggest[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement